Shell tutorial 3-shell special Variables

Source: Internet
Author: User
As mentioned above, variable names can only contain numbers, letters, and underscores, because some variables that contain other characters have special meanings. Such variables are called special variables.

For example, $ indicates the ID of the current shell process, that is, PID. See the following code:
Copy text-only new window
 
  1. $ Echo $
$echo $$
Running result
29949

Special Variable list
Variable Description
$0 File Name of the current script
$ N Parameters passed to the script or function. N is a number that represents the number of parameters. For example, the first parameter is $1, and the second parameter is $2.
$ # The number of parameters passed to the script or function.
$ * All parameters passed to the script or function.
[Email protected] All parameters passed to the script or function. The double quotation marks ("") are slightly different from $.
$? The exit status of the previous command or the return value of the function.
$ The ID of the Current Shell Process. Shell scripts are the process IDs of these scripts.
The parameter passed to the script when the command line parameter is run is called the command line parameter. The command line parameters are represented by $ n. For example, $1 indicates the first parameter, $2 indicates the second parameter, and so on.

See the following script:
Copy text-only new window
 
  1. #! /Bin/bash
  2.  
  3. Echo "File Name: $0"
  4. Echo "first parameter: $1"
  5. Echo "first parameter: $2"
  6. Echo "quoted values: [email protected]"
  7. Echo "quoted values: $ *"
  8. Echo "Total number of parameters: $ #"
#!/bin/bashecho "File Name: $0"echo "First Parameter : $1"echo "First Parameter : $2"echo "Quoted Values: [email protected]"echo "Quoted Values: $*"echo "Total Number of Parameters : $#"
Running result:
$./test.sh Zara AliFile Name : ./test.shFirst Parameter : ZaraSecond Parameter : AliQuoted Values: Zara AliQuoted Values: Zara AliTotal Number of Parameters : 2
$ * And [email protected] $ * and [email protected] indicate all parameters passed to the function or script. If they are not included in double quotation marks, all use "$1" "$2 "... All parameters are output in the form of "$ N.

But when they are enclosed by double quotes (""), "$ *" takes all parameters as a whole, with "$1 $2... All parameters are output in the form of $ n. "[email protected]" separates parameters and uses "$1" "$2 "... All parameters are output in the form of "$ N.

The following example shows the difference between $ * and [email protected:
Copy text-only new window
 
  1. #! /Bin/bash
  2. Echo "\ $ * =" $ *
  3. Echo "\" \ $ * \ "=" "$ *"
  4.  
  5. Echo "\ $ @ =" [email protected]
  6. Echo "\" \ $ @ \ "=" "[email protected]"
  7.  
  8. Echo "print each Param from \ $ *"
  9. For VaR in $ *
  10. Do
  11. Echo "$ Var"
  12. Done
  13.  
  14. Echo "print each Param from \ $ @"
  15. For VaR in [email protected]
  16. Do
  17. Echo "$ Var"
  18. Done
  19.  
  20. Echo "print each Param from \" \ $ *\""
  21. For VaR in "$ *"
  22. Do
  23. Echo "$ Var"
  24. Done
  25.  
  26. Echo "print each Param from \" \$ @\""
  27. For VaR in "[email protected]"
  28. Do
  29. Echo "$ Var"
  30. Done
#!/bin/bashecho "\$*=" $*echo "\"\$*\"=" "$*"echo "\[email protected]=" [email protected]echo "\"\[email protected]\"=" "[email protected]"echo "print each param from \$*"for var in $*do    echo "$var"doneecho "print each param from \[email protected]"for var in [email protected]do    echo "$var"doneecho "print each param from \"\$*\""for var in "$*"do    echo "$var"doneecho "print each param from \"\[email protected]\""for var in "[email protected]"do    echo "$var"done
Run./test. Sh "A" "B" "C" "D". The following result is displayed:
$*=  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 param from [email protected]abcdprint each param from "$*"a b c dprint each param from "[email protected]"abcd
Exit Status $? You can get the exit status of the previous command. The exit status is the result returned after the previous command is executed.

The exit status is a number. Generally, if most commands are successfully executed, 0 is returned. If the command fails, 1 is returned.

However, some commands return other values, indicating different types of errors.

In the following example, the command is successfully executed:
$./test.sh Zara AliFile Name : ./test.shFirst Parameter : ZaraSecond Parameter : AliQuoted Values: Zara AliQuoted Values: Zara AliTotal Number of Parameters : 2$echo $?0$

$? It can also represent the return value of the function, which will be explained later.

Shell tutorial 3-shell special 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.