A detailed explanation of the variable types in the Linux bash Shell _linux Shell

Source: Internet
Author: User

In the Linux system for day-to-day operations or scripting, variables are more familiar with, but what are the types of variables, what is the difference in the specific usage? This paper is to share with you:

One, bash variable type:

Local variables
Local variables
Environment variables
Position variable
Special variables (built-in)

Second, local variables:

Varname=value: Scopes are available for the entire bash process;

Variable Naming conventions:

1. Can only contain letters, numbers and underscores, and begins with letters and underscores
2. It is best not to have the same name as the environment variables
3. See the name of the known meaning

Third, local variables:

Local Varname=value: Scope is current code segment;

Four, environment variable:

The scope is the current shell process and its child processes and cannot affect its parent process;
Export Varname=value "Exporting", if the variable has been defined can only be used with variable name export varname, i.e.
1. Export Varname=value
2. Varname=value
Export VarName
The script starts a child shell environment variable when the command is executed:
Scripts that are automatically executed by the system (not command-line startup) need to customize the environment variables required;

V. Position variables:

$1,$2,$3, ...
Cat first.sh Test.txt Hello
$1:first.sh
$2:test.txt
$3:hello
Shift: Kick off the parameters
Shift N: Kick out n parameters and kick off a parameter by default

VI. SPECIAL variables:

$?: The return value of the previous command execution state:

There may be two kinds of return values for program execution:
1. Results of program implementation
2. Does the program state return (0-255)
0 to perform the correct
1-255 Execution Error (1,2,127 system reservation);

$#: Gets the total number of arguments in the current shell command line
$*: Get all the arguments for the current shell "$ $ ..., controlled by IFS
$@: All parameters of this program "$1″" $2″ "$3″" ... ", not under the IFS control
$ Gets the file name of the currently executing shell script
$n gets the nth parameter value of the currently executing shell script, n=1..9
$$ Gets the process number (PID) of the current shell
$! PID that executes the previous instruction

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.