If the expression contains special characters, shell will replace it. For example, using variables in double quotes is a replacement, and escape characters are also a replacement.
For example:
Copy text-only new window
- #! /Bin/bash
-
- A = 10
- Echo-e "value of A is $ A \ n"
#!/bin/basha=10echo -e "Value of a is $a \n"
Running result:
Value of a is 10
Here,-E replaces the escape characters. If the-e option is not used, the output is as follows:
Value of a is 10\n
The following escape characters can be used in ECHO:
Escape characters |
Description |
\\ |
Backslash |
\ |
Alarm, bell |
\ B |
Return (delete key) |
\ F |
Change page (ff), move the current position to the beginning of the next page |
\ N |
Line feed |
\ R |
Enter |
\ T |
Horizontal tab (Tab key) |
\ V |
Vertical Tab |
You can use the-E Option of the ECHO command to disable escape. By default, escape is not allowed. You can use the-n option to disable line breaks.
Command replacement
Command replacement means that the shell can first execute the command to temporarily Save the output result and output it in a proper place.
Syntax of command replacement:
Copy text-only new window
- 'Command'
`command`
Note that the quotation marks are not single quotes. The key is located below the ESC key.
In the following example, the command execution result is saved in the variable:
Copy text-only new window
- #! /Bin/bash
-
- Date = 'date'
- Echo "date is $ date"
-
- Users = 'who | WC-l'
- Echo "logged in user are $ users"
-
- Up = 'date; uptime'
- Echo "uptime is $ up"
#!/bin/bashDATE=`date`echo "Date is $DATE"USERS=`who | wc -l`echo "Logged in user are $USERS"UP=`date ; uptime`echo "Uptime is $UP"
Running result:
Date is Thu Jul 2 03:59:57 MST 2009Logged in user are 1Uptime is Thu Jul 2 03:59:57 MST 200903:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15
Variable replacement
Variable replacement can be used to change the value of a variable based on its State (whether it is empty or defined ).
Possible variable replacement formats:
Form |
Description |
$ {Var} |
Original variable value |
$ {Var:-word} |
If the VaR variable is null or has been deleted (unset), the return word will not change the value of var. |
$ {Var: = word} |
If the VaR variable is null or has been deleted (unset), the system returns the word and sets the VaR value to word. |
$ {Var :? Message} |
If the variable VAR is null or has been deleted (unset), send the message to the standard error output to check whether the variable VAR can be assigned a value normally. If this replacement occurs in the shell script, the script stops running. |
$ {Var: + word} |
If the variable VAR is defined, word is returned, but the value of VaR is not changed. |
See the following example:
#!/bin/bashecho ${var:-"Variable is not set"}echo "1 - Value of var is ${var}"echo ${var:="Variable is not set"}echo "2 - Value of var is ${var}"unset varecho ${var:+"This is default value"}echo "3 - Value of var is $var"var="Prefix"echo ${var:+"This is default value"}echo "4 - Value of var is $var"echo ${var:?"Print this message"}echo "5 - Value of var is ${var}"
Running result:
Copy text-only new window
- Variable is not set
- 1-value of VaR is
- Variable is not set
- 2-value of VaR is variable is not set
-
- 3-value of VaR is
- This is default value
- 4-value of VaR is prefix
- Prefix
- 5-value of VaR is prefix
Shell tutorial 4-shell replacement