If the expression contains special characters, the Shell will replace it. For example, using a variable in double quotes is a substitution, and an escape character is also a substitution.
As an example:
- #!/bin/bash
- A=ten
- echo- e "Value of A is $a \ n"
Operation Result:
Value of A is 10
Here-e indicates the substitution of the escape character. If you do not use the-e option, it is output as-is:
Value of A is 10\n
The following escape characters can be used in echo:
Escape Character |
meaning |
\\ |
Back slash |
\a |
Alarm, Bell |
\b |
BACKSPACE (delete key) |
\f |
Page Break (FF), moving the current position to the beginning of the next page |
\ n |
Line break |
\ r |
Enter |
\ t |
Horizontal tab (Tab key) |
\v |
Vertical tab |
You can suppress escaping by using the-e option of the Echo command, which is not escaped by default, or by using the-N option to prevent the insertion of line breaks.
Command substitution
Command substitution means that the shell can execute commands first, save the output temporarily, and output it where appropriate.
Syntax for command substitution:
- 'command '
Note that it is an anti-quote, not a single quote, which is below the ESC key.
In the following example, the command execution results are saved in a variable:
- #!/bin/bash
- Date= ' Date '
- Echo "Date is $DATE"
- USERS= ' Who | wc- l '
- echo "Logged in user is $USERS"
- Up= ' date ; uptime '
- Echo "Uptime is $UP"
Operation Result:
Date is Thu Jul 2 03:59:57 mst 2009Logged in user be 1Uptime is Thu Jul 2 03:59:57 MST 200903:59:57 up 14:03, 1 user, load avg:0.13, 0.07, 0.15
Variable substitution
Variable substitution can change its value depending on the state of the variable (whether it is empty, whether it is defined, etc.)
Variable substitution forms that you can use:
form |
Description |
${var} |
The variable's original value |
${var:-word} |
If the variable var is empty or has been deleted (unset), then return to word, but do not change the value of var. |
${var:=word} |
If the variable var is empty or has been deleted (unset), return to Word and set the value of Var to word. |
${var:?message} |
If the variable var is empty or has been deleted (unset), then send message messages to the standard error output, which can be used to detect whether Var can be properly assigned. If this substitution appears in the shell script, the script will stop running. |
${var:+word} |
If the variable var is defined, it returns word but does not change the value of var. |
Take a look at the following example:
#!/bin/bashecho ${var:-"Variable is not set"}echo "1-value of Var is ${var}" echo ${var:= "Variable are not set"}echo "2- Value of Var is ${var} "unset Varecho ${var:+" This is the default value "}echo" 3-value of Var is $var "var=" Prefix "echo ${var: + "This is the default value"}echo "4-value of Var is $var" Echo ${var:? " Print this message "}echo" 5-value of Var is ${var} "
Operation Result:
- Variable is not set
- 1-value of Var is
- Variable is not set
- 2-value of Var is Variable are not set
- 3-value of Var is
- The IS default value
- 4-value of Var is Prefix
- Prefix
- 5-value of Var is Prefix
Shell Script Learning 9 Shell replacement: Shell variable substitution, command substitution, escape character