Shell Variables   «Prev  Next»

Using Variable Substitution Syntax

  1. ${#variable}: Return the lenght of the variable (number of characters).
  2. ${variable}: Return the value of the variable (method used to avoid ambiguous variable naming).
  3. $variable: Return the value of the varialbe (standard method of referencing a variable).
  4. ${variable:-response}: Return response if the variable is not set.
  5. ${variable:=response}: Return response if the variable is not defined and set the variable equal to response.
  6. ${variable:?response}: Return response as an error messge if the variable has not value.
  7. ${variable:+response}: Return response if the variable is set, otherwise return a blank string.

Variable Substitution Syntax

Following is the example, while printing value of the variable its substitued by its value. Same time "\n" is substituted by a new line -
#!/bin/sh
a=10
echo -e "Value of a is $a \n"

This would produce following result. Here -e option enables interpretation of backslash escapes.
Value of a is 10
Here is the result without -e option:

Value of a is 10\n