Linux Shell Script:basic concept01-string

Source: Internet
Author: User

Basic concept01:string
All of the tests in this article, such as no special instructions, are based on fish shell

Let's start with the string, what is the string, and we'll look at a few simple examples

?> ~ set param ABC? Echo " string with blank and $param surrounded with double quotation marks " string Double quotation marks? Echo ' string with blank and $param surrounded with single quotation marks ' string with blank and $param surrounded with single quotation marks

Notice the difference after the $parm unfolds? Here comes the first concept I'm going to talk about.
Concept Shell string Substitution:
Single-quote string, Shell does not do string substitution
Double quote string, Shell to do string substitution


This is the difference between a single quote string and a double-quoted string, so what is the difference between a string without single quotation marks or double quotation marks?
Look at the following example

Echo " $param " ABC? Echo $PARAMABC

All two commands replace the string (the variable expands), and the two commands perform exactly the same. So what's the difference between a string without a single quote/double quote?
Let's go into bash and write an example.

[Email protected]:~$ x=123[email protected]-virtual-machine:~$ y=123 456No Command'456'found, did you mean:command'A56'From package'A56'(Universe)456: Command not found[email protected]-virtual-machine:~$ z="123 456"[email protected]-virtual-machine:~$Echo$x123[email protected]-virtual-machine:~$Echo$y [email protected]-virtual-machine:~$Echo$z123 456[email protected]-virtual-machine:~$

Do you see the assignment report syntax for the y variable? A string with no single/double quotation marks, with a space/carriage return as the beginning and end of the string, in addition to the double-quote string
OK, let's take a moment to sum up, version of concept 01:
1. The shell will replace the string
2. Shell uses single quotes, double quotes to identify the border of a string, when there is no single/double quotation marks, with a space/carriage return as border
3. Single quote string does not do string substitution, the other two do string substitution

What strings will the shell replace? Let's continue to look at a few examples

?> ~set param ABC?> ~Echo "$param"ABC?> ~Echo "\ $param"$param?> ~Echo(ls) Desktop Documents Downloads examples.desktop linux_shell.txt Music Pictures public pycharmprojects pycharm Register Keys record software Templates Videos workspace?> ~[email protected]-virtual-machine:~$Echo`ls' Desktop Documents Downloads examples.desktop linux_shell.txt Music Pictures public pycharmprojects pycharm Register Keys record software Templates Videos workspace[email protected]-virtual-machine:~$

Note that the last example is executed in bash, and ' ls ' uses an inverse quotation mark , which is the key next to key 1 (not the keypad 1) (do not press shift)
Do you understand? The inverted-quote string, which is substituted with parentheses in fish, is the output of the program. Let's continue to summarize, version of concept 01:
1. The shell will replace the string: variable substitution ($param), escape character substitution (\$), program standard output substitution (' ls ')
2. Shell uses single quotes, double quotes to identify the border of a string, when there is no single/double quotation marks, with a space/carriage return as border
3. Single quote string does not do string substitution, the other two do string substitution

CONCEPT01 The basic introduction is finished. Yes, this series is written in my years of technical understanding and summary, naturally not only a little superficial content.
So next, let's look at a few more examples:
Sample 01:

?> ~ Set X (Echohello)?> ~Echo$xhello?> ~set Y (AAA) No command'AAA'found, did you mean:command'jaaa'From package'jaaa'(Universe) Command'AA'From package'Astronomical-almanac'(Universe) Command'AHA'From package'AHA'(Universe) Command'Ara'From package'Ara'(Universe) Aaa:command not found?> ~Echo$y?> ~

See, the shell only uses the program's standard output (stdout) to replace the string with () or the back quotation marks, while stderr is the output information, but it is not used to replace
Version of concept01:
1. The shell will replace the string: variable substitution, escape character substitution, program standard output substitution (only stdout, exclude stderr)
2. Shell uses single quotes, double quotes to identify the border of a string, when there is no single/double quotation marks, with a space/carriage return as border
3. Single quote string does not do string substitution, the other two do string substitution

Sample 02:

?> ~Echo "ABC 123"ABC123?> ~Echo "ABC \ "123\""ABC"123"?> ~Echo "ABC ' 123 '"ABC'123'?> ~Echo 'ABC 123'ABC123?> ~Echo 'ABC \ ' 123\ ''ABC'123'?> ~Echo 'ABC "123"'ABC"123"?> ~

1. Single quotation marks in string escaping character \ ' will be replaced
2. Double quotation marks in string escaping character \ "will be replaced
3. Single quotation marks and double quotation marks can be nested using
Let's take a moment to summarize:
Version of CONCEPT01
1. The shell will replace the string: variable substitution, escape character substitution, program standard output substitution (only stdout, exclude stderr)
2. Shell uses single quotes, double quotes to identify the border of a string, when there is no single/double quotation marks, with a space/carriage return as border
3. Single-Quote strings do not replace strings, and the other two do string substitution. However, single quotes in single-quote strings need to use escape characters and are replaced. Double quotes in a double-quoted string require an escape character

Sample 03:

?> ~ set param ABC? ' $param ' ? Echo " $p " $param? > ~

The value of the $p $param is not replaced by recursion
Version of Concept01:
1. The shell replaces the string: variable substitution, escape character substitution, program standard output substitution (only stdout, exclude stderr). No recursive substitution
2. Shell uses single quotes, double quotes to identify the border of a string, when there is no single/double quotation marks, with a space/carriage return as border
3. Single-Quote strings do not replace strings, and the other two do string substitution. However, single quotes in single-quote strings need to use escape characters and are replaced. Double quotes in a double-quoted string require an escape character

Sample 04:

Echo 1 (echo2 (echo3  ABC))123 ABC

version of Concept01:
1. The shell replaces the string: variable substitution, escape character substitution, program standard output substitution (only stdout, exclude stderr). No recursive substitution
2. Shell uses single quotes, double quotes to identify the border of a string, when there is no single/double quotation marks, with a space/carriage return as Border
3. Single-Quote strings do not replace strings, and the other two do string substitution. However, single quotes in single-quote strings need to use escape characters and are replaced. Double quotes in a double-quoted string require an escape character
4. Parentheses/anti-quotation marks can be nested using

The analysis of the string concept in the shell is over, it's not complicated, you think?

the difficulty with strings in shell script is that in addition to the shell itself replacing strings, commands, especially grep egrep sed, which use regular expressions, also replace strings, which can be truly confusing when the process is mixed.
This is the next article to be told.

The second primer:
Question 1:
Since strings with no single double quotes are border with a space/carriage return, then why do the following two sets of commands have the same effect:
?> ~ Echo ABC 123
ABC 123
?> ~ set param a string with blank, but without single/double qutation marks
?> ~ Echo $param
This was a string with blank, but without single/double qutation marks
?> ~

?> ~ echo "ABC 123"
ABC 123
?> ~ set param "This was a string with blank, but without single/double qutation marks"
?> ~ Echo $param
This was a string with blank, but without single/double qutation marks
?> ~

Question 2:
Take a picture of the following commands
?> ~ Echo abc.def abcxdef | grep ' Abc.def '--color=auto
Abc.def Abcxdef
?> ~ Echo abc.def abcxdef | grep ' Abc\.def '--color=auto
Abc.def Abcxdef
?> ~
?> ~ Echo abc.def abcxdef | grep ' *.def '--color=auto
?> ~ Echo abc.def abcxdef | Grep-e ' *.def '--color=auto
Abc.def Abcxdef
?> ~

Linux Shell Script:basic concept01-string

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.