Parameter expansion in Bash

The ‘$’ character is used for parameter expansion, command substitution, or arithmetic expansion. It is very useful if you want to manipulate the values of variables without using some other external command like sedawkpython etc. In this article you will learn more about how to use parameter expansion in Bash.

First of all, is important to tell some things about the syntax.

In this example, you can see some basic syntax of bash parameter expansion. We assigned a value to dir variable. In the next lines, we used “$” to expand the value for the symbol dir. As you can see, the expansion can be used in any kind of contexts. The parameter name or symbol to be expanded may be enclosed in braces (check the last line of the example), which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name. When using braces not to create ambiguity, the matching ending brace is the first ‘} not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

Default shell variable value

Parameter expansion can be used to set a default value for a shell variable. If the value is not provided, then the interpreter will use the default value for the variable. This is useful when you want to make your script runnable even without providing any parameters.

The script from the example takes as a parameter a name for a directory and some permissions. Permissions have a default value set to ‘0755’. The script checks if a directory name was provided but does not care if the user does not provide some value for permissions, because it will use the default value.

To set a default value for a variable you can also use this syntax:

${varName:=val}

This syntax will set value val to varName if varName does not have any value set. The differece between this syntax and the syntax from the example with directory creation is that the := will not work with positional parameter arguments.

Display error if value not set

To display an error and stop the script execution if the value for a variable is not set, you can use the following syntax:

dirName="${1:?Usage: ./script.sh /name/of/dir}"

Length of variables

In case of strings or arrays, you can easily find the length of a variable using the following syntax:

var="Hello World!"
len=${#var}
echo "The length is: ${len}"

Process values using patterns (RegEx)

To remove a substring from the front of a value you can use the following syntax:

  ${var#Pattern}
  ${var##Pattern}
  # example: get only the file name from a path
  src="/var/log/nginx/access.log"
  echo "${src##*/}"

To process a string and strip a substring from the back you can use this syntax:

  ${var%Pattern}
  ${var%%Pattern}
  # Example: remove extension
  var="archive.tar.gz"
  echo "Name: ${var%.tar.gz}"

Replace substring in a string value

To avoid the use of external commands like sed or awk you can exploit parameter expansion to replace a substring in a string, based on a provided pattern.

  ${var/Pattern/ReplacementString}
  # Example: replace hello with greeting
  input="Hello, World!"
  echo "New string is: ${input/Hello/Greeting}"

Converting char case (Upper <-> Lower)

  name="KernelPanic"
  echo "UPCASE: ${name^}"
  echo "Lowercase: ${name,,}"
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...