Real-time scenarios where VARIABLE SUBSTITUTION/EXPANSION is useful


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Real-time scenarios where VARIABLE SUBSTITUTION/EXPANSION is useful
# 1  
Old 06-06-2015
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
# 2  
Old 06-06-2015
Quote:
Originally Posted by ab_2010
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:

walking back up a directory path

Storing string space

Not to forget the obvious application:

Code:
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
# 3  
Old 06-07-2015
If othervar is not set
Code:
var=${othervar:=defaultvalue}

assigns defaultvalue to both var and othervar. I have always used
Code:
var=${othervar:-defaultvalue}

Also I cannot see that
Code:
: ${var:=defaultvalue}

has any advantage over
Code:
var=${var:-defaultvalue}

so the use cases of the := are rare.
# 4  
Old 06-07-2015
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:
# 5  
Old 06-07-2015
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):
Code:
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.
Code:
if [ ${var:=0} -ge 1 ]; then


Last edited by Scrutinizer; 06-07-2015 at 09:12 AM..
# 6  
Old 06-08-2015
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):

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

# 7  
Old 06-08-2015
Probably my most frequent use is to get the filename from a full path, essentally replacing the basename command:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies

2. Shell Programming and Scripting

Converting real time to epoch time

# 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)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

Shell script to convert epoch time to real time

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)
Discussion started by: aismann
4 Replies

4. Shell Programming and Scripting

[Solved] Command Substitution and Variable Expansion within a Case

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)
Discussion started by: jaimielives
1 Replies

5. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

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)
Discussion started by: glev2005
8 Replies
Login or Register to Ask a Question