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

Source: Internet
Author: User
Location parameters in bash shell $ #, $ *, $ @, $0, $1, $2... and special parameters $ ?, Meaning of $-and so on


In bash shell

Often
When you see some special symbols, I will collect them for reference:

Location parameters:

For details, see ABS (Advanced bash
Shell) The internal variables in chapter 1 of chapter 1 of Page 103 of the Chinese translation version are the same in the English version of ABS.

$1, $2, $3, etc...
Location Parameter, from
The command line is passed to the script, or to the function, or to a variable.




This number can be any number, but only the first
9
Can be accessed, use
Shift
Command to change this restriction.

(For details, see Example 4-5 and example 11-15)




$0



$0 indicates

When
The name of the previously executed process, the script name, or the whole line output in a regular expression.

$ #

Command Line or Location Parameter
Number (see example 33-2)
$ *

Institute
Some 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,
Not explained or expanded
This also means that each parameter in each parameter list is treated as an independent word.
Note: "$ @" must be



""



Reference.



Other special parameters

$-

The falg passed to the script (use the set command). Refer to example 11-15.
Display
Shell
The current option used
Set
Command functions are the same

Note: This was initially a feature of KSh and was introduced to bash later.
But unfortunately, in bash it does not look like
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
Temporary File names for "unique.
(See example A-13, example 29-6, example 12-28 and example
11-25 ).
This is generally easier than calling mktemp.

Note
Meaning:
[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 meaning of a word is exactly the same.
Minutes, all translated into parameters)

Exit and return

Exit status
(Exit status)
The function returns a value called the exit state. The exit state can be specified by return. Otherwise, the function
Return
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
The 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 creator of two numbers
################### Start script #################
1
#! /Bin/bash
2 # Max. sh: the vertex in two integers.
3
4 e_param_err =-198 #
If the number of parameters passed to the function is less than two, the return value is returned.
5 equal =-199 # return value if two integers are equal.
6 #
Any parameter value passed to the function overflows.
7 #
8
9 max2 () # returns the greater value of two integers.
10 {# Note:
The number of participants in the comparison 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 to say, 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.