Some special $ headers in Shell

Source: Internet
Author: User
$0 script execution name
$ N the nth parameter value of the script, n = 1 .. 9
$ * All parameters of the script
$ @ All the parameters of the script (which have similarities and differences with $)
$ # Script parameter count
$ PID for Script Execution
$! PID used to execute the previous background command
$? The Return Value of the previous command.
When you execute the script with more than 9 parameters, you can use the shift command to move the parameters forward to one grid, so that you can use 10th parameters later. In addition, you can use the set command to change $ N and $ *. The method is: Set string is a value of $ *, which is put into $ N after decomposition. If the set command is followed by no parameter, all the preset variables and their values are listed.
File Name: AA. Sh this is a test
Echo filename: $0 filename: AA. Sh
Echo arguments: $ * arguments: this is a test
Echo No. of argS: $ # No. of argS: 4
Echo 2nd Arg: $2 2nd Arg: Is
Shift
Echo No. of argS.: $ # No. of argS: 3
Echo 2nd Arg.: $2 2nd Arg:
Set hello, everyone
Echo arguments: $ * arguments: Hello, everyone
Echo 2nd Arg.: $2 2nd Arg: everyone

For $ * and $ @ running my. Sh p1 "p2 P3" P4 on command line, either $ @ or $ *,
Get P1 P2 P3 P4. But if it is referenced in the program:
"$ @" You can get the three different words (Word) "p1" "p2 P3" "P4 )﹔
"$ *", You can get a single string of words "P1 P2 P3 P4. (The difference is not obvious. Reading NF using awk is equal to 4)
For a big difference, see the following code: AA. Sh
My_fun (){
Echo "$ #"
}
Echo 'number of param in "$ @" = '$ (my_fun "$ @")
Echo 'number of param in "$ *" = '$ (my_fun "$ *")
Then run AA. Sh p1 "p2 P3" P4 and the result is:
Number of param in "$ @" = 3
Number of param in "$ *" = 1

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.