Explanation of the meaning of shell variable $#,$@,$0,$1,$2 in Linux

Source: Internet
Author: User

The meaning of shell variable $#,[email protected],$0,$1,$2 in Linux is explained:
Variable Description:
$$
The PID of the shell itself (ProcessID)
$!
PID of the Shell's last running background process
$?
End code of the last Run command (return value)
$-
Flag at a glance using the SET command
$*
All parameter lists. such as "$*" in the Case of "" ", in the form of" $ $ ... $n "output all parameters.
[Email protected]
All parameter lists. such as "[email protected]" with "" "in the case, with" $ "" $ "... All parameters are output in the form "$n".
$#
Number of arguments added to the shell
$
The name of the shell itself
$1~ $n
Each parameter value added to the shell. $ $ is the 1th parameter, and the $ = is the 2nd parameter ....
Let's start by writing a simple script that will explain the meaning of each variable after execution.
# Touch Variable
# VI Variable
The script reads as follows:
#!/bin/sh
echo "number:$#"
echo "Scname:$0"
echo "First: $ $"
echo "Second:$2"
echo "Argume:[email protected]"
Save exit
Assigning Script Execution permissions
# chmod +x Variable
Execute script
#./variable AA BB
Number:2
Scname:./variable
First:aa
Second:bb
ARGUME:AA BB
By displaying the results you can see:
$# is the number of arguments passed to the script
The name of the script itself
$ $ is the first parameter passed to the shell script
$ $ is the second parameter passed to the shell script
[Email protected] is a list of all parameters passed to the script

Location parameters in Bash Shell $#,$*,[email protected],$0,$1,$2 ... and the meaning of special parameters $?,$-, etc.

Some of the more special symbols are often found in the bash shell and are now collected for review:

Position parameters:
See ABS (Advanced Bash Shell) Chinese Translation 103 page The first section of the 9th chapter internal variables, of course, the English version of ABS are the same

$, $, $ $, etc...
Positional parameters, passed from the command line to the script, or passed to a function. or assign a variable.
This number can be as many as possible, but only the first 9 can be accessed, using the shift command to change the limit.
(see example 4-5 and Example 11-15 for details)
$
$ A represents the currently executing process name, the name of the script itself, or the entire row of output in a regular expression

$#
The command line or the number of positional arguments. (see example 33-2)
$*
All positional parameters are used as a word.
Note: "$*" must be referenced by "".
[Email protected]
Synonymous with $*, but each parameter is a separate "" reference string, which means that the parameters are passed through completely,
is not explained and expanded. This also means that each parameter in each parameter list is treated as a separate word.
Note: "[email protected]" must be referenced.
Other special parameters
$-
The FALG passed to the script (using the SET command). Reference Example 11-15.
Displays the current options used by the shell, same as the SET command function
Note: This was originally a feature of Ksh and was later introduced into bash, but unfortunately, in bash it does not look
able to work reliably. One possible way to use it is to have the script self-test (see if it is a cross-
Mutual).
$!
The PID (process ID) of the last work that runs in the background.
$_
Saves the last parameter of the command that was executed before.
$?
The exit state of the command, function, or script itself (see example 23-7)
Used to check the last command, function or script execution is correct. (In Linux, a command exit status of 0 indicates that the command executes correctly, and any non-0 value indicates a command error.) )
$$
The process ID of the script itself. This variable is often used to construct a "unique" temporary file name.
(Refer to Example a-13,example 29-6,example 12-28 and Example 11-25).
This is usually simpler than calling Mktemp.
Precautions:
[1] The PID of the currently running script is $$.
[2] The two words "argument" and "parameter" are often used without distinction. In this whole book, these two
The meaning of the word is exactly the same. (no distinction in translation, all translated into parameters)

Exit and return
Exit State (Exit status)
The function returns a value that is called the exit state. Exit status can be specified statement by return, otherwise the function's
The exit status is the exit state of the last execution command of the function (0 indicates success, not 0 indicates an error code). Exit status
(exit status) can be in the script by $? Reference. This mechanism allows script functions to have a "C" function as well.
return value ".
Return
Terminates a function. return command [1] optional strip An integer parameter that returns as the function's "return value"
To the script calling this function, and this value is also assigned to the variable $?.

While true can be written as while:



Example 23-7 The largest of two numbers
################## #Start script#################
1 #!/bin/bash
2 # max.sh: The largest of two integers.
3
4 e_param_err=-198 # If the parameter passed to the function is less than 2, the return value.
5 equal=-199 # If two integer values are equal to the return value.
6 # Overflow of any parameter value passed to a function
9 m
8
9 max2 () # Returns a large value of two integers.
10 {# Note: The number of participating comparisons must be less than 257.
If [-Z "$"]
Then
return $E _param_err
+ fi
15
If ["$"-eq "$"]
Then
Return $EQUAL
Else
If ["$"-gt "$"]
Then
return $
All else
return $
Fi
+ fi
27}
28
33 34 Max2
Return_val=$?
31
If ["$return _val"-eq $E _param_err]
Then
echo "need to pass, parameters to the function."
elif ["$return _val"-eq $EQUAL]
Then
"The numbers is equal."
$ else
"The larger of the numbers is $return _val."
+ fi
41
42
Exit 0
44
45 # Exercise (easy):
#---------------
47 # Turn this script into an interactive script,
48 #+ that is, let the script ask the caller to enter 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.