Location parameters in Shell $ #, $ *, $ @, $0, $1, $2... And special parameters $ ?, Meaning of $-and so on

Source: Internet
Author: User

Some special symbols are often seen in bash shell. I collect them for reference:

Location parameters:
For more information, see ABS (Advanced bash shell). The English version of ABS is the same.

$0, $1, $2, and so on...
Location parameter, which is passed to the script from the command line, to the function, or to a variable.
(For details, see Example 4-5 and example 11-15)
$0 indicates the name of the currently executed process, the script name, or the whole line output in the regular expression.

$ #
Number of command lines or location parameters (see example 33-2)
$ *
All location parameters are used as a word.
Note: "$ *" must be referenced.
$ @
It is synonymous with $ *, but each parameter is an independent "" reference string, which means that the parameter is completely passed,
It is not interpreted and expanded. This also means that each parameter in each parameter list is considered as an independent word.
Note: "$ @" must be referenced.

 

Other special parameters
$-
The falg passed to the script (use the set command). Refer to example 11-15.
Note: This was initially a feature of KSh and was introduced to bash, but unfortunately it does not look like it in bash.
One possible way to use this script is to perform self-testing (check whether it is a token ).
Mutual ).
$!
The PID (process ID) of the final job running in the background ).
$ _
Save the last parameter of the previously executed command.
$?
Command, function, or script exit status (see example 23-7)
Used to check whether the previous command, function or script is correctly executed. (In Linux, if the exit status of a command is 0, the command is correctly executed. If the value is not 0, an error occurs .)
$
The ID of the script's own process. This variable is often used to construct a temporary file name for "unique.
(Refer to example A-13, example 29-6, example 12-28 and example 11-25 ).
This is generally easier than calling mktemp.
Note:
[1] The PID of the currently running script is $.
[2] The words "argument" and "parameter" are often used without distinction. In this book, the two
The meanings of words are exactly the same. (no distinction is added during translation, and all words are translated as parameters)

Exit and return
Exit status)
The function returns a value called the exit state. The exit state can be specified by return. Otherwise, the function
The exit status is the exit status of the Last Command executed by the function (0 indicates success, and non-0 indicates error code). The exit status
(Exit status) in the script? Reference. This mechanism allows the Script Function to have"
Return Value ".
Return
Terminate a function. return command [1] can take an integer parameter. This integer is returned as the "return value" of the function.
To the script that calls this function, and the value is also assigned to the variable $ ?.

While true can be written as while:

Example 23-7 The operator of two numbers
################### Start script #################
1 #! /Bin/bash
2 # Max. sh: the vertex in two integers.
3
4 e_param_err =-198 # return value if the number of parameters passed to the function is less than two.
5 equal =-199 # return value if two integers are equal.
6 # overflow of any parameter value passed to the Function
7 #
8
9 max2 () # returns the greater value of two integers.
10 {# Note: The number of participating comparisons must be less than 257.
11 if [-z "$2"]
12 then
13 return $ e_param_err
14 fi
15
16 if ["$1"-EQ "$2"]
17 then
18 return $ equal
19 else
20 if ["$1"-GT "$2"]
21 then
22 return $1
23 else
24 return $2
25 fi
26 FI
27}
28
29 max2 33 34
30 return_val =$?
31
32 if ["$ return_val"-EQ $ e_param_err]
33 then
34 echo "need to pass two parameters to the function ."
35 Elif ["$ return_val"-EQ $ equal]
36 then
37 echo "the two numbers are equal ."
38 else
39 echo "the larger of the two numbers is $ return_val ."
40 fi
41
42
43 exit 0
44
45 # exercise (easy ):
46 #---------------
47 # convert the script into an interactive script,
48 # + that is, the script allows the caller to input two integers.
##################### End Script ################# #

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.