Shell (1)

Source: Internet
Author: User
Tags aliases

Bash variable Category:

Local variable: Also called local variable, valid only for the current shell process

Environment variables: current shell and its child shell, child shell ....

Special variables: $? The status of the last command execution, and 0 indicates that execution succeeded. There are a lot of failed code.

[Email protected]:/'cm'config-manager'  Package [ Email protected]echo $? 127 [email protected] Echo $? 0

Variable assignment:

[Email protected]:/$ age=[email protected]echo  $age

Assign the value of a variable to another variable:

[Email protected]:/$ age=[email protected]-virtual-machine:/$ age1=$age [email protected]  echo  $age 1

Now there is a question, what is the value of age and age1 after executing the above code? Will the value of age1 change with age? Here is a new value for age to see

[Email protected]:/$ age=[email protected]echo  $age[email Protected]echo  $age 1

Now look at the following code, when the value of age is present, assign the value of age to a variable, and there is no value assigned to the variable

[Email protected]:/$ result=${age-}[email protected]echo  $result [email protected]-virtual-machine:/$ result=${age2-}[email protected]echo $result  +

An error message is returned when the age value is present when it is assigned and does not exist:

[Email protected]:/$ result=${age?' Error ' }[email protected]echo  $result[email protected]-virtual-machine:/$ Result=${age2?' Error ' }bash:age2:error

Think about what the above code does? Try it.

[Email protected]:/$ result=${age3?' Age3 does not exist ' }bash:age3:age3 does not exist

Output:

Echo $age Echo " Hello " #输出后不换行 Echo ' Hello '    #让转义符生效 echo'hello\tworld '   Echo " Hello\nworld "

Single quotes: Strong references, strings in quotes no matter what, intact

Double quotation marks: Weak references, the variables in the quotation marks are replaced with the values of the variables

Anti-quote: is the character on the keyboard ~ below, the command replaces

[Email protected]:/$ age= at[email protected]-virtual-machine:/$Echo 'I am $age'I am $age [email protected]-virtual-machine:/$Echo "I am $age"I am at[email protected]-virtual-machine:/$Echo "today is ' date +%f '"Today is .- on- -

A reference to a variable

${varname}

Curly braces can sometimes be omitted, sometimes not, take a look

[Email protected]:/$ age=[email protected]echo  $age[email Protected]-virtual-machine:/$ animal=pig[email protected]echo"there is some ${ Animal}s"there is some pigs[email protected]echo"there is Some $animals"there is some

Why does the last output display variable have no value? Because the animals is recognized as a variable.

Revocation of variables:

[Email protected]:/$ age=[email protected]echo  $age[email Protected]-virtual-machine:/$ unset age[email protected]echo $age

Read-only variable: cannot be undone, cannot be modified

[Email protected]:/$ readonly animal=pigbash:animal: Read-only variable [email protected]-virtual-machine:/$ unset Animalbash:unset:animal: Unable to reset: read-only variable

Declaration of environment variables:

[Email protected]:/$ export Age

You can also assign a value at the time of declaration

[Email protected]:/$ export Age

environment variable name generally all uppercase

Here are a few commands:

#查看所有环境变量的命令 (3) Env Printenvexport

New environment variable Path

Export Path=/usr/local/apache/bin: $PATH

Command substitution:

Echo " Today is ' date +%f ' "  -protected [email]echo 'date +%f-%h:%m: %S-(+)-£ º

Generating sequences

 [email protected]:/$ seq  3  Span style= "COLOR: #800080" >1  2  3  Span style= "COLOR: #000000" >[email protected] -virtual-machine:/$ seq  3  6   3  4  5  6  [email protected] -virtual-machine:/$ seq  1  3    1  4  7  10  

The environment variables generated by export will be lost after the shell restarts or restarts the system, then we can save the environment variables through the configuration file

#全局文件/etc/profile,   /etc/profile.d/*   ,/etc/bashrc# personal files ~/.bash_profile   ~/. BASHRC

Configuration file load Order

#交互式/etc/profile     /etc/profile.d/*     /bash_profile    ,     ~/.BASHRC/   etc /bashrc# non-interactive ~/.BASHRC     /ETC/BASHRC    /etc/profied/* #如果同一个环境变量存在多个配置文件中, whichever is the last loaded configuration file

Configuration file Categories

Profile class: Setting environment variables, running commands or scripts (before users log on or before bash starts)

BASHRC class: Setting local variables, setting command aliases

After the configuration file modification is complete, you need to use the following command to make it work, such as:

Souce ~/.bash_profile

Command aliases:

vi=vimalias cdnet='cd/etc/sysconfig/network-scripts/'   VI    #撤消别名

Enter redirection:

Command < file

Output redirection

Command > File

1 is the standard output 2 is the error output

Command 2>file

Correct and Error Redirect:

Command > File 2>&1 #都输出到file

Command > File 2 > File2 #输出到不同的文件

Command &>file Merge output stream

Pipeline:

Command | Command2 #把第一个命令的输出当作第二个命令的输入

Command | Command2 | Command3 | ... | CommandN

It is important to note that the pipeline only passes the correct execution result, and it terminates when an error is encountered.

[Email protected]:/$Echo "ABC"|TR "A- z" "A- z"Abc[email protected]-virtual-machine:/$Echo 'AbCd'|TR 'ABCD' 'ABCD'ABCD

Record a command

Cut–d ' –f2 the string with a space and outputs a second

Cut–d:–f2 to: Cut

Uniq to move a duplicate row

There are times when you encounter this situation:

ls /var/log | file  file [-bchikllnnprsvz0] [--apple] [--mime-encoding] [--mime-type]            [file  ...        file -C [-m magicfiles       ]file [--help]

Because file must be followed by a document, there is no way, in which case we can use-instead of

ls /var/log | file -/dev/stdin:ascii text

File is the type of view files

and record a command tee.

Tee /tmp/tee. Out |command2    #将command的结果保存到tee. Out and then passed to Command2

Shell (1)

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.