Variable reference and command replacement in Linux

Source: Internet
Author: User

Variable reference and command replacement in Linux

In bash script writing, we often need to reference variables and replace commands. In order to perform standard operations, we will briefly describe them.

Reference

A reference is to enclose a string with a reference symbol to prevent special characters from being interpreted as other meanings by shell scripts. The special meaning of a special character is shielded during reference and interpreted as literal.

References, names, and meanings

Quote Name Meaning
'' Single quotes It is called full reference or weak reference. All characters in single quotes are interpreted as literal meaning except single quotes. Single quotes do not support variable reference .. The single quotation mark is used to keep the literal value of all characters in the quotation marks, even if the \ and carriage return in the quotation marks are no exception.
"" Double quotation marks It is called a partial reference or a strong reference. It refers to all characters except the dollar sign ($), quotation mark ('), and backslash. That is, the special meanings of the dollar sign ($), backquotes ('), and backslash (\) are kept in double quotation marks, for example, "$ variable name" indicates replacing the variable name with the variable value. Variables referenced in double quotation marks can prevent string segmentation and retain spaces in variables.
'' Anti-quote Shell interprets the content in the anti-citation as a system command
\ Backslash Escape the special meanings of the next character. Common special characters in Linux include $, *, ', +, ^, &, | ,",?

Example: [root @ CentOS6 tmp] # test = "x y z"; echo '$ Test'

[Root @ CentOS6 tmp] # $ test # does not translate. Single quotes only output the literal meaning of the characters in it.
[Root @ CentOS6 tmp] # test = "x y z"; echo $ test
[Root @ CentOS6 tmp] # x y z # reference the variable value without retaining spaces in the Variable
[Root @ CentOS6 tmp] # test = "x y z"; echo "$ test"
[Root @ CentOS6 tmp] # x y z # reference the variable value and retain spaces in the Variable
Note: The value of a variable can be part of a long string. if it is at the end of a long string, it can be referenced directly. If it is at the beginning or in the middle, enclose the variable with curly brackets.
Example: [root @ CentOS6 tmp] # test = "xyz"; echo test $ test
[Root @ CentOS6 tmp] # testxyz # normal output


[Root @ CentOS6 tmp] # test = "xyz"; echo $ testtest
[Root @ CentOS6 tmp] # No output, because shell will reference the value of the variable testtest, but no value is defined for this variable.

[Root @ CentOS6 tmp] # test = "xyz"; echo $ {test} test
[Root @ CentOS6 tmp] # xyztest # normal output

Command replacement
Command replacement is to assign the standard output of a command as a value to a variable. bash Shell defines two forms of command replacement. The syntax format of the two forms is as follows:
Method 1: 'linux command'
Method 2: $ (Linux Command)

Example: [root @ CentOS6 tmp] # echo 'pwd'
[Root @ CentOS6 tmp] #/tmp # reference the execution result of the pwd command and Output

Note: Although $ () and reverse quotation marks are equivalent to command replacement, command replacement in the form of $ () can be nested.
In bash Shell, there is a difference between quotation marks and $ () when dealing with double backslash symbols.
[Root @ CentOS6 tmp] # echo \\
[Root @ CentOS6 tmp] #\# output Escape Character
[Root @ CentOS6 tmp] # echo 'echo \\'
[Root @ CentOS6 tmp] # output blank lines
[Root @ CentOS6 tmp] # echo $ (echo \\)
[Root @ CentOS6 tmp] #\# output single slash

This article permanently updates the link address:

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.