Linux variable reference and command substitution

Source: Internet
Author: User

In bash scripting, we often need to reference variables and replace the command, for the specification operation, it is now a simple summary of the description.


Reference

Referencing refers to enclosing strings in reference notation to prevent special characters from being interpreted by shell scripts as other meanings. The special meaning of shielding special characters when referenced, and it is interpreted as literal meaning.

Reference symbol, name, meaning description table

Reference character Name Meaning description
‘‘ Single quotation marks All characters are referred to as full or weak references, and characters in single quotes are interpreted as literal meanings except for the single quotation mark itself, and single quotes do not have the function of referencing variables. Single quotation marks are used to hold the literal value of all the characters in the quotation marks, even if the \ and carriage returns within the quotation marks are no exception.
"" Double quotes Called a partial or strong reference that references all characters except the dollar sign ($), the anti-quote ('), and the backslash (\). That is, the special meaning of the dollar sign ($), the inverse quotation mark ('), and the backslash (\) symbol in double quotation marks, such as "$ variable name" means replacing the variable name with the variable value. using double quotation marks to reference a variable prevents the string from being split, preserving the spaces in the variable.
`` Anti-Primer The shell interprets the contents of the backslash as a system command
\ Back slash The signifier, shielding the special meaning of the next character, Linux commonly used special characters are $, *, ', +, ^, &, |, ",?

Example: [[email protected] tmp]# test= "x y z"; Echo ' $test '

               [[email protected] tmp]# $test       #不会转意, single quotes simply output the literal meaning of the characters inside

& nbsp              [[email protected] tmp]#  test= "x   y      z"; Echo $test

< Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " >               [[email protected] tmp]#   x   y   z            #引用变量值, but the spaces in the variable are not preserved

< Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " > < Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " >               [[email protected] tmp]#   test= "x   y      z", echo "$test"

< Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " >               [[email protected] tmp]#   x   y      z       #引用变量值, and leave the space 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, and if it is at the beginning or the middle, enclose the variable in curly braces.

          Example: [[[email protected] tmp]# test= "xyz"; Echo test$test

< Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " >                [[email protected] tmp]# testxyz         & nbsp;  #正常输出

< Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " >

[[email protected] tmp]# test= "xyz"; Echo $testtest

[[email protected] tmp]# #不会有任何输出, because the shell will refer to the value of the variable testtest, the variable does not define an assignment

< Span style= "Color:rgb (51,51,51); Font-family:arial;font-size:14px;line-height:26px;background-color:rgb ( 255,255,255); " >

[[email protected] tmp]# test= "xyz"; Echo ${test}test

[Email protected] tmp]# xyztest #正常输出


Command substitution

Command substitution refers to assigning the standard output of a command to a variable as a value, and the bash shell defines two forms of substitution for commands, the syntax of which is as follows:

Mode one: ' Linux command '

Way two: $ (Linux command)

Example: [[email protected] tmp]# echo ' pwd '

[email protected] tmp]#/tmp #引用pwd命令的执行结果并输出


Note: Although $ () and anti-quotes are equivalent on command substitution, command substitution in the form of $ () can be nested .

in bash shell, there is a difference between the anti-quote and $ () when dealing with double backslash symbols.

[[email protected] tmp]#echo \ \

[[email protected] tmp]#\ #输出转义符

[[email protected] tmp]#echo ' echo \ \ '

[[email protected] tmp]# #输出空白行

[[email protected] tmp]# #echo $ (echo \ \)

[[email protected] tmp]#\ #输出单斜线



This article is from the "Flying Snail" blog, please be sure to keep this source http://ljmsky.blog.51cto.com/2878/1629401

Linux variable reference and command substitution

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.