Linux Shell Parameter Expansion

By:    Updated: February 27,2017

One core functionality of Bash is to manage parameters. A parameter is an entity that stores values and is referenced by a name, a number or a special symbol. Linux Parameter Expansion is the procedure to get the value from the referenced entity, like expanding a variable to print its value. On expansion time you can do very nasty things with the parameter or its value.

Linux Parameter Expansion

Expression Meaning
$param get value of param
${param} The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.
   
${param-$default} If param is unset, return the value of default. Otherwise, return the value of param.
${param:-$default} If param is unset or null(aka empty), return the value of default. Otherwise, return the value of param.
   
${param=$default} If param is unset, assign the value of default to param, and then return it. Otherwise, return the value of param.
${param:=$default} If param is unset or null, assign the value of default to param, and then return it. Otherwise, return the value of param.
   
${param+$default} If param is unset, nothing is returned, otherwise (is set) return the value of default.
${param:+$default} If param is unset or null, nothing is returned, otherwise (is set and not null) return the value of default.
   
${param?$default} If parameter is unset, the value of default is written to the standard error and print. If the shell is not interactive, abort script with an exit status of 1. Otherwise, return the value of param.
${param:?$default} If parameter is unset or null, the value of default is written to the standard error and print. If the shell is not interactive, abort script with an exit status of 1. Otherwise, return the value of param.
   
${!param} The referenced parameter is not param itself, but the parameter whose name is stored as the value of param. If the parameter param has the value "flasle", then ${!param} will expand to the value of the parameter named flasle (the same as ${flasle}).
   
${!param*} This expands to a list of all set variable names beginning with the string param. The elements of the list are separated by the first character in the IFS-variable (<space> by default).
${!param@} This expands to a list of all set variable names beginning with the string param. The elements of the list are separated by the first character in the IFS-variable (<space> by default).When '@' is used and the expansion appears within double quotes, each variable name expands to a separate word.
   
${!param[*]} If param is an array variable, expands to the list of array indices (keys) assigned in param. If param is not an array, expands to 0 if param is set and null otherwise.
${!param[@]} If param is an array variable, expands to the list of array indices (keys) assigned in param. If param is not an array, expands to 0 if param is set and null otherwise.When '@' is used and the expansion appears within double quotes, each key expands to a separate word.

 

Source Code:

#!/bin/bash

#Role:    Linux Shell Parameter Expansion
#Author:  Flasle <valinv.com@gmail.com>
#Created: 2017.02.27

default="www.valinv.com"

#------Simple usage--------------
param_1="flasle"

echo -e "\r\nSimple usage-------------------------------\r"
echo -e "\$param_1 is $param_1 \r"
echo -e "\$param_1s is $param_1s \r"
echo -e "\${param_1}s is ${param_1}s \r"

#------Use a default value------------
param_2="flasle"
#param_2_1      #param unset
param_2_2=""    #param empty(aka null)

echo -e "\r\nUse a default value-------------------------\r"
echo -e "\${param_2-\$default} is ${param_2-$default} \r"
echo -e "\${param_2:-\$default} is ${param_2:-$default} \r"
echo -e "\${param_2_1-\$default} is ${param_2_1-$default}, current param_2_1 is ${param_2_1} \r"
echo -e "\${param_2_1:-\$default} is ${param_2_1:-$default}, current param_2_1 is ${param_2_1} \r"
echo -e "\${param_2_2-\$default} is ${param_2_2-$default}, current param_2_1 is ${param_2_1} \r"
echo -e "\${param_2_2:-\$default} is ${param_2_2:-$default}, current param_2_1 is ${param_2_1} \r"

#------Assign a default value---------
param_3="flasle"
#param_3_1      #param unset
param_3_2=""    #param empty(aka null)

echo -e "\r\nAssign a default value----------------------\r"
echo -e "\${param_3=\$default} is ${param_3=$default} \r"
echo -e "\${param_3:=\$default} is ${param_3:=$default} \r"
echo -e "\${param_3_1=\$default} is ${param_3_1=$default}, current param_3_1 is ${param_3_1} \r"
echo -e "\${param_3_1:=\$default} is ${param_3_1:=$default}, current param_3_1 is ${param_3_1} \r"
echo -e "\${param_3_2=\$default} is ${param_3_2=$default}, current param_3_2 is ${param_3_2} \r"
echo -e "\${param_3_2:=\$default} is ${param_3_2:=$default}, current param_3_2 is ${param_3_2} \r"

#------Use an alternate value---------
param_4="flasle"
#param_4_1      #param unset
param_4_2=""    #param empty(aka null)

echo -e "\r\nUse an alternate value----------------------\r"
echo -e "\${param_4+\$default} is ${param_4+$default} \r"
echo -e "\${param_4:+\$default} is ${param_4:+$default} \r"
echo -e "\${param_4_1+\$default} is ${param_4_1+$default}, current param_4_1 is ${param_4_1} \r"
echo -e "\${param_4_1:+\$default} is ${param_4_1:+$default}, current param_4_1 is ${param_4_1} \r"
echo -e "\${param_4_2+\$default} is ${param_4_2+$default}, current param_4_2 is ${param_4_2} \r"
echo -e "\${param_4_2:+\$default} is ${param_4_2:+$default}, current param_4_2 is ${param_4_2} \r"

#------Indirection------------------
param_6="flasle"
flasle="This is a value of flasle after name expansion"

echo -e "\r\nIndirection----------------------------------\r"
echo -e "\${!param_6} is '${!param_6}'"

#------Variable name expansion 1------------------
param_7_1="flasle_7_1"
param_7_2="flasle_7_2"
param_7_3="flasle_7_3"
param_7_4="flasle_7_4"
param_7_5="flasle_7_5"

echo -e "\r\nVariable name expansion 1----------------------\r"
echo -e "A list of all set variable names beginning with the string param_7(expression: \${!param_7*}): \r"
echo -e "${!param_7*} \r"
echo -e "A list of all set variable names beginning with the string param_7(expression: \${!param_7@}): \r"
echo -e "${!param_7@} \r"

echo -e "\r\n"

echo -e "element list with \${!param_7*}: \r"
offset_7=0
for var in "${!param_7*}"
do
	echo -e "element_${offset_7}: ${var} \r"
	((offset_7++))
done

echo -e "element list with \${!param_7@}: \r"
offset_7=0
for var in "${!param_7@}"
do
	echo -e "element_${offset_7}: ${var} \r"
	((offset_7++))
done

#------Variable name expansion 2------------------
param_8=("flasle_8_1" "flasle_8_2" "flasle_8_3" "flasle_8_4")

echo -e "\r\nVariable name expansion 2----------------------\r"
echo -e "The list of array indices (keys) assigned in param_8 with \${!param_8[*]}: \r"
echo -e "${!param_8[*]}"
echo -e "The list of array indices (keys) assigned in param_8 with \${!param_8[@]}: \r"
echo -e "${!param_8[@]}"

echo -e "\r\n"

echo -e "key list with \${!param_8[*]}: \r"
offset_8=0
for var in "${!param_8[*]}"
do
	echo -e "key_${offset_8}: ${var} \r"
	((offset_8++))
done

echo -e "key list with \${!param_8[*]}: \r"
offset_8=0
for var in "${!param_8[@]}"
do
	echo -e "key_${offset_8}: ${var} \r"
	((offset_8++))
done

#------Display error if null or unset--
param_5="flasle"
#param_5_1      #param unset
param_5_2=""    #param empty(aka null)

echo -e "\r\nDisplay error if null or unset----------------\r"
echo -e "\${param_5?\$default} is ${param_5?$default} \r"
echo -e "\${param_5:?\$default} is ${param_5:?$default} \r"
#echo -e "\${param_5_1?\$default} is ${param_5_1?$default}, current param_5_1 is ${param_5_1} \r"
#echo -e "\${param_5_1:?\$default} is ${param_5_1:?$default}, current param_5_1 is ${param_5_1} \r"
echo -e "\${param_5_2?\$default} is ${param_5_2?$default}, current param_5_2 is ${param_5_2} \r"
echo -e "\${param_5_2:?\$default} is ${param_5_2:?$default}, current param_5_2 is ${param_5_2} \r"

Output:

Simple usage-------------------------------
$param_1 is flasle 
$param_1s is  
${param_1}s is flasles 

Use a default value-------------------------
${param_2-$default} is flasle 
${param_2:-$default} is flasle 
${param_2_1-$default} is www.valinv.com, current param_2_1 is  
${param_2_1:-$default} is www.valinv.com, current param_2_1 is  
${param_2_2-$default} is , current param_2_1 is  
${param_2_2:-$default} is www.valinv.com, current param_2_1 is  

Assign a default value----------------------
${param_3=$default} is flasle 
${param_3:=$default} is flasle 
${param_3_1=$default} is www.valinv.com, current param_3_1 is www.valinv.com 
${param_3_1:=$default} is www.valinv.com, current param_3_1 is www.valinv.com 
${param_3_2=$default} is , current param_3_2 is  
${param_3_2:=$default} is www.valinv.com, current param_3_2 is www.valinv.com 

Use an alternate value----------------------
${param_4+$default} is www.valinv.com 
${param_4:+$default} is www.valinv.com 
${param_4_1+$default} is , current param_4_1 is  
${param_4_1:+$default} is , current param_4_1 is  
${param_4_2+$default} is www.valinv.com, current param_4_2 is  
${param_4_2:+$default} is , current param_4_2 is  

Indirection----------------------------------
${!param_6} is 'This is a value of flasle after name expansion'

Variable name expansion 1----------------------
A list of all set variable names beginning with the string param_7(expression: ${!param_7*}): 
param_7_1 param_7_2 param_7_3 param_7_4 param_7_5 
A list of all set variable names beginning with the string param_7(expression: ${!param_7@}): 
param_7_1 param_7_2 param_7_3 param_7_4 param_7_5 


element list with ${!param_7*}: 
element_0: param_7_1 param_7_2 param_7_3 param_7_4 param_7_5 
element list with ${!param_7@}: 
element_0: param_7_1 
element_1: param_7_2 
element_2: param_7_3 
element_3: param_7_4 
element_4: param_7_5 

Variable name expansion 2----------------------
The list of array indices (keys) assigned in param_8 with ${!param_8[*]}: 
0 1 2 3
The list of array indices (keys) assigned in param_8 with ${!param_8[@]}: 
0 1 2 3


key list with ${!param_8[*]}: 
key_0: 0 1 2 3 
key list with ${!param_8[*]}: 
key_0: 0 
key_1: 1 
key_2: 2 
key_3: 3 

Display error if null or unset----------------
${param_5?$default} is flasle 
${param_5:?$default} is flasle 
${param_5_2?$default} is , current param_5_2 is  
./valinv_www_lpe.sh: line 128: param_5_2: www.valinv.com

 

More in Development Center
New on Valinv
Sources
  • http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
Related Articles
Sponsored Links