Shell substitution: Shell variable substitution, command substitution, escape character

Source: Internet
Author: User
Tags delete key echo command

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:

    1. #!/bin/bash
    2. A=ten
    3. 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:

    1. '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:

    1. #!/bin/bash
    2. Date= ' Date '
    3. Echo "Date is $DATE"
    4. USERS= ' Who | wc- l '
    5. echo "Logged in user is $USERS"
    6. Up= ' date ; uptime '
    7. 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:

    1. Variable is not set
    2. 1-value of Var is
    3. Variable is not set
    4. 2-value of Var is Variable are not set
    5. 3-value of Var is
    6. The IS default value
    7. 4-value of Var is Prefix
    8. Prefix
    9. 5-value of Var is Prefix

Shell substitution: Shell variable substitution, command substitution, escape character

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.