Shell script Note (ii) Shell variables

Source: Internet
Author: User

Shell variable one) Global environment variables

A global variable is visible to both the shell that defines it and its child shell, but if the shell that generates it is terminated, the global variable disappears. In addition, the global variables are overwritten with the same variable as the shell.

#定义一个全局变量export a="I wantBreak Free"#或者b="Sunday Morning  "export b# Delete a global variable unset a
II) Local Environment variables

Local environment variables can only be visible in the process in which they are defined.

There is no space between the #注意变量名, equal sign, and value c="Live Forever"unset c

Distinguishing global environment variables from local environment variables in fact, we just need to remember that the global environment variable is scoped to a more shell.

In addition, the variables in the script, defined by the local variables in the entire scope is the entire script file, including the variables you define in the method. To produce a "block-level scope" variable, you need to add a local to the variable definition

Local d="Lucyinthe sky"
III) Persistence of variables

Environment variables to be used by all users under Linux:

Scenario One: Put the variable into the/etc/profile--------fatal flaw: If the upgrade of the Linux system/etc/profile file is updated, the set of variables no longer exist.

Scenario Two: Create a file in the/ETC/PROFILE.D directory that ends in. SH and write the variable. This method is recommended.

Environment variables to be used by individual users:

Put in: ~/.BASHRC file

Four) array variables
#定义array=(one tow three four) #使用 echo ${array[0]} #threeecho ${ array[*]} #one tow three four# delete unsetarray[1]unsetarray[*]
V) Special variables

1) Positional parameter variables

$--------------------------------can get the current script file name, if the script is executed with a path, the resulting value when the path

$n---------------------------------N is a number and must be in the form of ${10} when n exceeds 9. Gets the nth parameter of the execution script

$*---------------------------------Get all the parameters of the current script, with double quotes equivalent to "$ $"

[email protected] without quotation marks when the function and non-quoted $*, the same quotation mark is equivalent to "$" "$" "$"

2) Special state variables in the process

$?--------------------------------to get the return value after the last instruction was executed

$$--------------------------------The process number that executes the shell script

VI) Processing of variables

1. System built-in commands

1) Echo: Output variable command format on screen echo args where arges can be a combination of variables and strings

2) Eval:

Command format: eval args

Function: When the shell program executes to the Eval statement, the shell reads in the parameter args and combines them into a new command.

3) Exec:

Command format: EXEC command parameters

Function: If you do not create a new child process, go to execute the specified command, and specify that the command is finished to terminate the process.

4) Read:

Command format: read [parameter] [variable name] (the variable cannot be a special variable)
Function: Reads a string from a standard input and passes it to a variable defined inside the shell.

Ten '  ' num

5) Shilft:

Command format: shilft-shilft positional parameters

Function: Each time the SHILFT statement is used in the program, all positional parameters are moved to the left one position, and the leftmost variable is deleted.

6) Exit:

To exit the shell, you can select a number as the return value.

2. Handling Variable Content

Shell script Note (ii) Shell variables

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.