Real-time scenarios where VARIABLE SUBSTITUTION/EXPANSION is useful

Hi,

at academic level I am familiar with how variable substitution/expansion feature works. From your live experience, can you please tell what are real-time scenarios where ${variable:=} ${variable%pattern} ${variable:=word} sort of features can be used? How can we connect that dot that this feature is useful in this scenario? One thing, I can think of is FULL-PATH-NAME related stripping etc. please share your thoughts.

thank you

First off, I think it is one of the defining qualities of a software engineer to come up with useful applications of a language feature once he gets to know it. By analogy, Beethoven had the same 12 tones to work with as any other but what made him a composer was that he started having ideas on how to combine them in an original and inventive way.

Here are, off the top of my head, some threads where i mentioned applications for variable expansion:

walking back up a directory path

Storing string space

Not to forget the obvious application:

var="${othervar:=defaultvalue}"

which assigns a variable a default value in case "$othervar" is not set so far. This way you can avoid lengthy checks for completeness of sets of input parameters.

Finally, you can use variable expansion for everything where you would normally use string functions in other languages or string-manipulating commands (grep, awk, sed, tr, ...) in shell. Variable expansion is very fast and even if the code is ten times as long it will perhaps be executed faster than any external program. I learned this lesson myself right here.

I hope this helps.

bakunin

If othervar is not set

var=${othervar:=defaultvalue}

assigns defaultvalue to both var and othervar. I have always used

var=${othervar:-defaultvalue}

Also I cannot see that

: ${var:=defaultvalue}

has any advantage over

var=${var:-defaultvalue}

so the use cases of the := are rare.

MadeInGermany, you are right. You are right too in pointing out that ":-" is perhaps more common than ":=". I just used this as - one - example out of a millions applications for parameter expansion in general because the threads o/p has mentioned it explicitly in his post #1.

I didn't undergo too much effort in explaining what a specific expansion could be used for because - see the first part of my answer - i think what sets apart programmers from the non-programmers is the ability to look at a certain language feature (or function, tool, ...) and come up with an idea of its useful application. People who see a certain stone and do not envision a sculpture IMHO do not have what it takes to be a sculptor either.

bakunin

3 Likes

One practical application for ${parameter:=word} I can think of is is with the use of $PWD which is not always defined in every shell, whereas `pwd` is alway available (as long as the search path is set correctly in the script).

However, the latter is expensive since it involves a subshell and an external utility.
By using this construct we can use it multiple times (e.g. in a loop):

printf "%s\n" "${PWD:=`pwd`}"

And still be sure that if PWD is not set, `pwd` will only be called once in the script.

Likewise for the COLUMNS variable, etcetera..

---
Another possible application is when using numerical comparisons to make sure that a variable contains a numerical value.

if [ ${var:=0} -ge 1 ]; then

I tend to use ${variable%pattern} most frequently to remove extensions from filenames. Second most common usage is probably splitting up strings (date or time strings for example):

year=${date%%[/ -]*}
day=${date##*[/ -]}
month=${date#*[/ -]}
month=${month%[/ -]*}

Probably my most frequent use is to get the filename from a full path, essentally replacing the basename command:

path=/path/to/file
file=${path##*/}