Several environment variables and parameter variables for Shell programming

Source: Internet
Author: User
Several environment variables and parameter variables for Shell programming-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. Environment Variables

When the Shell script program starts to execute, some variables will be initialized according to the environment value. these variables generally use uppercase letters as names, and variables defined by the user in the script program generally Use lowercase letters as names.

The following are some common variables:

$ HOME current user's login directory
$ PATH search directory list with commands separated by colons
$ PS1 command line prompt, usually a "$" Character
$ PS2 auxiliary prompt, used to prompt subsequent input, usually ">" characters
$ IFS input delimiter. When the shell reads the input data, a group of characters are considered as the delimiter between words. They are usually spaces, tabs, and line breaks.
$0 name of the shell script program.
$ # Number of parameters passed to the script program.
$ Process ID of the shell script program, which is generally used to create a unique temporary file, such as/tmp/tmpfile _ $

2. parameter variables

If the script program still has parameters when calling it, some variables will be generated. Even if the script does not pass any parameters, the $ # mentioned above also exists, but its value is 0.

Common parameter variables are as follows:

$1, $2, $3... parameters of the script program
$ * A list of all parameters. These parameters are separated by the first character in the environment variable IFS.
$ @ It is a variable of "$ *". It does not use IFS environment variables.
The differences between "$ *" and "$ @" are described in the technical specifications of X/Open.
When the characters in the double brackets are replaced by name-value, "$ *" is extended as a data Field. IFS ("Internal Field Separator") is used for each parameter ") if you cancel the settings for IFS. it is separated by a space. if IFS is set to a null character (different from canceling its settings), the value of the parameter is joined. for example:
$ IFS =''
$ Set foo bar bam
$ Echo "$ @"
Foo bar bam
$ Echo "$ *"
Foobarbam
$ Unset IFS
$ Echo "$ *"
Foo bar bam
As shown in the preceding example, "$ @" in double quotes still extends each parameter to data separated from each other and is not affected by the variable value of IFS. generally, if you want to access the parameters of the script program, using "$ @" is a good choice.

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.