
Parameter expansion on a Linux system is a feature in the bash shell that allows you to manipulate the values of variables, but you need to use a specific syntax. They are often used in shell scripts, but they can also be used on the command line for tasks such as defaulting values and manipulating strings.
Here is a very simple example that returns “Hello” along with the value of the $name variable:
$ name="Bozo"
$ echo "Hello, ${name}!"
Hello, Bozo!
The following example uses a default value (Guest) if $name is not set or if it’s null. Note the use of braces – not parentheses – in these commands.
$ echo "${name:-Guest}" # Uses "name" if set, otherwise "Guest"
Guest
This command displays the variable $MYID and, if not set, defaults to “anonymous”.
$ echo ${MYID-anonymous}
anonymous
The following command displays a default value if a variable if it is not set.
$ echo ${var:=NOT_SET}
123456789
$ echo ${var2:=NOT_SET}
NOT_SET
The example below assigns “anonymous” to the “user” variable if it has no assigned value.
$ echo "${user:=anonymous}"
anonymous
The command below will print a “not set” message and exits if the $hostname variable is not set or if it’s null.
$ echo "${HOSTNAME:?HOSTNAME is not set}"
fedora
$ echo "${HOSTNAME2:?HOSTNAME2 is not set}"
-bash: HOSTNAME2: HOSTNAME2 is not set
You can display the length of a variable’s value with a command like this:
$ var="bananas"
$ echo ${#var}
7
In the example below, the number of characters in the variable $name will be displayed.
$ name="sandra"
$ echo ${#name)
6
The two commands below display substrings for the specified variable.
$ var="123456789"
$ echo ${var:1:3}
234
$ echo ${var:0:3}
123
In the example below, a substring of the $text variable is displayed. The 6 and 5 represent the offset and length of the text to be displayed.
$ text="I love Linux"
echo ${text:7:5}
Linux
Note that the 7th character of the string is a blank. Think of the string as starting with character 0.
$ string="0123456"
$ echo ${string:2:5}
23456
To remove specified text from the beginning or end of a variable’s value, use a command like one of these:
$ file="archive.tar.gz"
$ echo ${file%.gz} # removes .gz from the displayed file name (shortest match)
archive.tar
$ echo ${file%%.*} # removes everything after the first string
archive
Remove pattern match from the beginning of a variable:
$ echo ${text#You}
can be whatever you want to be
$ echo ${text#Y}
ou can be whatever you want to be
In the command shown below, the first and then all instances of the specified text are replaced.
$ text="You can be whatever you want to be"
$ echo ${text/be/do} # change first instance
You can do whatever you want to be
$ echo ${text//be/do} # change al instances
You can do whatever you want to do
The example below uses indirect expansion. A variable is created that points to another variable.
name="user"
user="Sandra"
echo ${!name}’
Sandra
Wrap-up
Parameter expansion requires a bit of practice, but it can be very useful once you’ve become familiar with its many applications.
Source:: Network World