Shell Special variables: $ A, $#, $*, $@, $?, $$, and command-line arguments

Source: Internet
Author: User

variable names can only contain numbers, letters, and underscores, because some variables that contain other characters have special meanings, and such variables are called special variables.


List of special variables
variables meaning
$ File name of the current script
$n

Arguments passed to the script or function. N is a number that represents the first few parameters.

For example, the first parameter is $ $, and the second argument is $ A.

$# The number of arguments passed to the script or function.
$* All parameters passed to the script or function.
[Email protected] All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below.
$? The exit state of the last command, or the return value of the function.
$$ The current shell process ID. For Shell scripts, this is the process ID where the scripts are located.

For example, $ represents the ID of the current shell process, the PID, as shown in the following code:

# echo $$

Run results

29949

One, command-line parameters

The arguments passed to the script when the script is run are called command-line arguments. Command-line arguments are represented by a $n, for example, $ = for the first argument, and $ for the second argument, and so on.

Take a look at the following script:

#!/bin/bashecho "File Name: $" echo "first Parameter:" Echo "first Parameter: $" echo "Quoted Values: [Email protected ] "echo" Quoted Values: $* "echo" Total number of Parameters: $# "

Operation Result:

#./test.sh Zara alifile Name:./test.shfirst parameter:zarasecond parameter:aliquoted Values:zara AliQuoted Values: Zara alitotal Number of Parameters:2

Second, $* and [email protected] the difference

$* 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".


The following example can clearly see the difference between $* and [email protected]:

  1. #!/bin/bash

  2. echo "\$*=" $*

  3. echo "\" \$*\ "= " "$* "


  4. echo "\$@=" [email protected]

  5. echo "\" \$@\"= " [email protected] "


  6. echo "Print each param from \$*"

  7. for var in $*

  8. Do

  9. echo "$var"

  10. Done


  11. echo "Print each param from \$@"

  12. for var in [email protected]

  13. Do

  14. echo "$var"

  15. Done


  16. echo "Print each param from \" \$*\""

  17. for var in "$*"

  18. Do

  19. echo "$var"

  20. Done


  21. echo "Print each param from \" \$@\""

  22. for var in "[email protected]"

  23. Do

  24. echo "$var"

  25. Done

Execute./test.sh "a" "B" "C" "D" and see the following result:

$*= a b c d "$*" = a b c d[email protected]= a b c d "[email protected]" = a b c dprint each param from $*abcdprint each par AM from [email protected]abcdprint to param from "$*" a b c dprint each param from "[email protected]" ABCD

Third, exit status

$? You can get the exit status of the previous command. The so-called exit status is the return result after the last command was executed.

Exit status is a number, in general, most of the command execution succeeds returns 0, and the failure returns 1.

However, there are some commands that return other values that represent different types of errors.

In the following example, the command executes successfully:

#./test.sh Zara alifile Name:./test.shfirst parameter:zarasecond parameter:aliquoted Values:zara AliQuoted Values: Zara alitotal number of parameters:2# echo $?0


$? You can also represent the return value of a function.


This article is from the "Wind No Trace" blog, please be sure to keep this source http://wangfx.blog.51cto.com/1697877/1940921

Shell Special variables: $ A, $#, $*, [email protected], $?, $$ and command line arguments

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.