Shell tutorial 4-shell replacement

Source: Internet
Author: User
Tags echo command

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
 
  1. #! /Bin/bash
  2.  
  3. A = 10
  4. 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
 
  1. '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
 
  1. #! /Bin/bash
  2.  
  3. Date = 'date'
  4. Echo "date is $ date"
  5.  
  6. Users = 'who | WC-l'
  7. Echo "logged in user are $ users"
  8.  
  9. Up = 'date; uptime'
  10. 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
 
  1. Variable is not set
  2. 1-value of VaR is
  3. Variable is not set
  4. 2-value of VaR is variable is not set
  5.  
  6. 3-value of VaR is
  7. This is default value
  8. 4-value of VaR is prefix
  9. Prefix
  10. 5-value of VaR is prefix

Shell tutorial 4-shell replacement

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.