Some Bash-isms

Being a “code daily” type of open-source SysAdmin/Operations Manager, I’ll post here a ‘tricks & tips’ that I’ve found useful when using and programming in the Bash Shell.

This one notes how to replace a Bash variable name with another variable name. I know that the best practice here is to use one of the many forms of Arrays that Bash provides, but this is applicable to a situation where an Array is not available, and there’s a number of Bash variables containing data that are able to have their variable names computed/calculated. eg: getting passed a shell or process’ environment to a Bash script.

For example/to demonstrate:

# initialise
first_last1=10 ; first_last2=20 ; first_last3=30
# process
count=1
while [ $count -le 3 ]; do
TMP="first_last$count"
echo Variable named first_last$count = ${!TMP}
let "count+=1"
done

Yields results of:

Variable named first_last1 = 10
Variable named first_last2 = 20
Variable named first_last3 = 30

FYI,
Richard.

This entry was posted in Network Presence, Rich and tagged , . Bookmark the permalink.