Shell 13th Q & A notes

Source: Internet
Author: User
Tags control characters echo command
Common shells are mainly divided into two major types:
SH:
Burne shell (SH)
Burne again shell (BASH)
CSH:
C shell (CSH)
TC shell (tcsh)
Korn shell (Ksh)
Bash is one of the most successful products in the GNU project. It has been widely used in UNIX since its launch.
Users love it and gradually become the system standard of many organizations.

ECHO in the default, after the argument is displayed, a new-line
Charactor ). To cancel the line feed, you can use echo's-N option, for example:
Wangnc> echo "test"
Test
Wangnc> echo-n "test"
Test wangnc>
Wangnc>
In fact, besides-N options, ECHO has the following common options:
-E: Enable the backslash to control character conversion (refer to the table below)
-E: Turn off the backslash to control character conversion (as preset)
-N: cancel the line break at the end of the line (agree with the \ c character under the-E Option)
The following table lists the backslash control characters supported by the echo command:
\ A: alert/Bell (send ringtone from system horn)
\ B: backspace, that is, the deletion key to the left.
\ C: cancel the line break symbol at the end of the line
\ E: Escape, skip key
\ F: formfeed, page feed character
\ N: newline, line feed character
\ R: return, enter
\ T: tab, table hop key
\ V: vertical tab, vertical table hop key
\ N: ASCII octal encoding (in hexadecimal format starting with X)
\: Backslash itself
Example:
Wangnc> echo "A \ Tb \ TC \ nd \ te \ TF"
A \ Tb \ TC \ nd \ te \ TF
Wangnc> echo-e "A \ Tb \ TC \ nd \ te \ TF"
A B C
D E F
Wangnc>

In addition to ifs and Cr, commonly used meta also includes:
=: Set variables.
$: Replace the variable or operation (do not mix it with shell prompt ).
>: Redirection to stdout.
<: Redirection to stdin.
|: Command pipeline.
&: Rewrite the file descriptor or place the command in the background for execution.
(): Place the commands in it in the nested subshell for execution, or use it for calculation or command replacement.
{}: Place the command in the non-named function for execution, or use it in the defined range of variable replacement.
;: When the previous command ends, ignore the returned value and continue to execute the next command.
&: When the previous command ends, if the returned value is true, continue to execute the next command.
|: When the previous command ends, if the returned value is false, continue to execute the next command.
! : Execute commands in the History List
....

In bash, there are three common quoting methods:
* Hard quote: ''(single quotes). All the Meta In the hard quote is disabled.
* Soft quote: "" (double quotation marks), a large part of the Meta in soft quoe
Will be closed, but some will be retained (for example, $ ). (Note 2)
* Escape: \ (backslash), which is only a single line after the escape (Escape Character)
Meta is disabled.
Example:
Wangnc> A = B C
-Bash: C: Command not found
Wangnc> A = B \ c
Wangnc> echo $
B c
Wangnc> echo "$"
B c
Wangnc> echo '$'
$
Wangnc> echo "'$ '"
'B C'
Wangnc> echo '"$ "'
"$"
Wangnc>

In Bash, you can use "=" to set or redefine the content of a variable:
Name = Value
When setting variables, follow the following rules:
* The left and right sides of the equal sign cannot use the ifs or shell reserved characters (meta
Charactor ).
* The variable name cannot use the $ symbol.
* The first letter of the variable name cannot be a number ).
* The variable name cannot contain more than 256 letters.
* The variable name and variable value are case sensitive ).

You can use the variable replacement capability of the command line to "Expand" (append) the variable value. We use the segmentation symbol (:
) To achieve the purpose of expansion, use {} to clearly define the range of variable names:
Wangnc> A = B: C: d
Wangnc> echo $
B: C: d
Wangnc> A = $ A: E
Wangnc> echo $
B: C: D: E
Wangnc> A = BCD
Wangnc> Ech $
-Bash: Ech: Command not found
Wangnc> echo $
BCD
Wangnc> A = $ AE
-Bash: AE: unbound variable
Wangnc> A =$ {A} e
Wangnc> echo $
Bcde
Wangnc>

Strictly speaking, all the variables defined in the current shell belong to the "local variable" (Local
Variable), can be an environment variable (Environment
Variable), when using export, do not forget Shell
Use the command line to process the variable "replace" (substitution), for example:
Wangnc> A = B
Wangnc> B = C
Wangnc> export $
The preceding command does not output a as an environment variable, but instead outputs B
This is because in this command line, $ A will first be swapped out of B and then "stuffed back" for export.
.

() And {} differences:
() Place the Command Group in sub-shell for execution, also known as nested sub-shell.
{} Is completed in the same shell, also known as non-named Command Group.
A little similar to fork and Source

$ @ What is the difference between $ * and $?
If you run my. Sh p1 "p2 P3" P4 on command line, whether it is $ @ or $ *
, You can get P1 P2 P3 P4. However, if it is placed in soft quotes:
"$ @", You can obtain the three different word segments "p1" "p2 P3" "P4 )﹔
"$ *", You can obtain the entire string of "P1 P2 P3 P4.

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.