Shell Scripting Lesson One: Shell Three quotes Learning

Source: Internet
Author: User

I. Declaration and reference of variables in the shell

[[email protected] ~]# Var1=hello[[email protected] ~]# echo $var 1hello[[email protected] ~]# echo ${var1}worldhelloworld

In the shell, a reference to a variable uses $.

In the above code, $var 1 is a reference to the variable var1, and the result of the output is hello.


If you want to link other characters, place the variable inside the curly braces {}, and then follow the rest of the content.

For example, in the code above: ${var1}world The final result is HelloWorld


Note: The shell is a weakly typed programming language, and it is not the value that is quoted in double quotes as a string. The difference between single quotes, double quotes, and anti-quotes is mainly in the parsing of variables and commands.


Ii. use of double quotation marks

Double quotes in SHLL can be used to parse variables.

[[email protected] ~]# var2= "$var 1" [[email protected] ~]# echo $var 2Hello

As the code shows: the reference to the variable var1 is placed in double quotes, and the value assigned to VAR2,VAR2 becomes the value of var1.

As you can see, double quotation marks can parse variables.


Three, single quotation marks

[[email protected] ~]# var2= ' $var 1 ' [[email protected] ~]# echo $var 2$var1

As you can see from the code above, the biggest difference between single quotes and double quotes is that single quotes cannot parse variables, and what is assigned to a variable is not parsed out because it is a variable.


Four, anti-quote

Anti-quotes are used primarily to parse commands

[Email protected] ~]# whoamiroot[[email protected] ~]# var= ' whoami ' [[email protected] ~]# echo $varroot

In the above code, WHOAMI is placed in single quotes and assigned to Var, then the value of Var is the value of the command WhoAmI, which means that the contents of the anti-quotation marks are parsed as commands.


[Email protected] ~]# var1= "WhoAmI" [[email protected] ~]# echo $var 1whoami[[email protected] ~]# var= ' $var 1 ' [[Email prot Ected] ~]# Echo $varroot

As can be seen from the above code, when there is a variable reference in the anti-quote, the variable is parsed out, and then the result of the variable parsed as a command to parse, and finally assigned to the variable var.


The above is the three kinds of quotes commonly used in shell scripts to learn.

This article is from the "ssspure" blog, make sure to keep this source http://ssspure.blog.51cto.com/8624394/1860423

Shell Scripting Lesson One: Shell Three quotes Learning

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.