Fundamentals of shell Programming for Linux

Source: Internet
Author: User

One, variable

Variables in the shell are divided into: local variables, environment variables, positional parameters;

Local variables: Variables that can only be used in scripts for the user's current shell lifetime, local variables are invalidated as the shell process dies, local variables are still invalid in the newly started shell, similar to the concept of local variables in C and C + +;

Environment variables: Applies to all the processes generated by the login process;

Positional parameters: Used to provide pass parameters to the shell script, which is read-only;

A variable is the name of a value, the value of a reference variable is called: variable substitution, the $ symbol is a variable substitution symbol, such as variable is the variable name, then $variable is the value of the expression variable;

Variable=value #切记: Spaces cannot appear on either side of the equals sign

${variable=value} #同上

Examples are as follows:

var1= "Hello World"

Var2= "I say"

Var3= "We are saying $var 1" #ok

Purge of variables: unset command

unset variable Name

unset var, echo $var, the result shows a blank line, which means that the Var variable is not initialized;

Variable Assignment mode:

Var=value #注意: cannot have spaces on either side of the equals sign

 1 1, variable initialized condition 2[Email protected]222- the- -- -:~/CP/SH# colour="Reb"3[Email protected]222- the- -- -:~/CP/SH#Echo "is ${colour+"Blue"}"4 is Blue5[Email protected]222- the- -- -:~/CP/SH#Echo$colour6 Reb7[Email protected]222- the- -- -:~/CP/SH# unset Colour82, the variable is not initialized, ": =" and ":-" for the initialized variable operation, no effect 9[Email protected]222- the- -- -:~/CP/SH# unset ColourTen[Email protected]222- the- -- -:~/CP/SH#Echo$colour One  A[Email protected]222- the- -- -:~/CP/SH#Echo "is ${colour:="Red"}" - is Red -[Email protected]222- the- -- -:~/CP/SH#Echo$colour the Red -[Email protected]222- the- -- -:~/CP/SH# unset Colour -[Email protected]222- the- -- -:~/CP/SH#Echo "is ${colour:-"Red"}" - is Red +[Email protected]222- the- -- -:~/CP/SH#Echo$colour -  +[Email protected]222- the- -- -:~/CP/SH#

  

Fundamentals of shell Programming for Linux

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.