Use of shell variables

Source: Internet
Author: User

Reprint please indicate http://www.cnblogs.com/winifred-tang94/

There are three types of variables in the shell environment:

A, environment variables; You can refer directly to the shell script in the form of "environment variable name".

b, user-defined variables, do not need to be defined in advance, usually, the use of variables to directly assign the initial value. Variable name = variable initial value (Note: Variable name requirement-the first character must be a letter, can be underlined, but cannot have punctuation and spaces, cannot use keywords in the shell)

C, System global variables, representing a specific meaning, can not be modified by the user. In general, the main global variables include the following:

$#: When executing shell scripts, the number of command-line arguments does not include the script itself.

$?: Executes the return value of the previous shell command (exit status, most command execution successfully returns 0, failure returns 1, but there are other commands that return other values to identify other errors).

The name of the $0:shell script program itself. At the same time, $ $ represents the first parameter in the shell command line, and $ $ represents the second argument, and $ $ represents the third argument, and so on.

$*: All parameters passed to the script or function.

$$: The current shell process ID, which is the process ID of the current script.

To use a variable in a script is a method that takes advantage of the "$ variable name".

Eg. Create a script to calculate the sum of two numbers

One notable point : When using expr for operations, remember to add spaces on both sides of the operator.

The result is:

The initial error is as follows:

Cause: This script file requires parameters other than the script itself, and no parameters are entered when the script is run.

The difference between $* and [email protected]:

$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n". But when they are enclosed in double quotation marks (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".

Eg.

Use of 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.