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.
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?
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:
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.
If othervar is not set
assigns defaultvalue to both var and othervar. I have always used
Also I cannot see that
has any advantage over
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
These 3 Users Gave Thanks to bakunin For This Post:
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):
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.
Last edited by Scrutinizer; 06-07-2015 at 09:12 AM..
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):
# date +%s -d "Mon Feb 11 02:26:04"
1360567564
# perl -e 'print scalar localtime(1360567564), "\n";'
Mon Feb 11 02:26:04 2013
the epoch conversion is working fine. but one of my application needs 13 digit epoch time as input
1359453135154
rather than 10 digit epoch time 1360567564... (3 Replies)
Dear experts,
I have an epoch time input file such as : -
1302451209564
1302483698948
1302485231072
1302490805383
1302519244700
1302492787481
1302505299145
1302506557022
1302532112140
1302501033105
1302511536485
1302512669550
I need the epoch time above to be converted into real... (4 Replies)
Hello All,
I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system).
I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other... (1 Reply)
I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename?
#!/bin/bash
filename=`echo $0`
/usr/bin/sed '/#include/ {
i\
the filename is `$filename`
}' $1
exit 0 (8 Replies)