What is the meaning of variable $#,$@,$0,$1,$2,$*,$$,$ in Linux?

Source: Internet
Author: User

$# is the number of arguments passed to the script. $ is the name of the script itself. $ is the first parameter passed to the shell script, and the second argument that is passed to the shell script is [email protected] is a list of all the parameters passed to the script $* is to display all parameters passed to the script in a single string, unlike positional variables, which can be more than 9 $$ is the current process ID number for the script to run? is to display the exit status of the last command, 0 means no error, others indicate an error

Difference: [Email protected] $*

    • Same point: All parameters are referenced
    • Different points: only in double quotes. Assuming that three parameters were written when the script was run (stored in $ $ $), "$*" is equivalent to "$ $" (passing a parameter) and "[email protected]" is equivalent to "$" "$" "$" (three parameters passed)

Example One

# #dels. Sh
Echo "number:$#"Echo "scname:$0"Echo "First : $"Echo "second:$2"Echo "Argume:[email protected]"Echo "Show Parm list:$*"Echo "Show Process id:$$"Echo "Show Precomm stat: $?"

Execution results

[@jihite]$ sh del.sh 1 2 3number:3scname:del.shfirst:1second:2argume:1 2 3show parm list:1 2 3show process Id:21057show p Recomm stat:0

Example Two

#!/bin/SHNum=$ #name=$0Echo "Number : $num"Echo "scname: $name"Echo$0Echo$1Echo$2 for((i=0; i< $num; i++)) Do    Echo "$i" DoneEcho "Argume:[email protected]" forKeyinch[email protected] Do    Echo$key DoneEcho "-----------------" forKeyinch "[email protected]" Do    Echo$key DoneEcho "-----------------------------" forKey2inch$* Do     Echo$key 2 DoneEcho "-----------------" forKey2inch "$*" Do     Echo$key 2 DoneEcho "Show Process id:$$"ChoEcho "Show Precomm stat: $?"

Execution results

[@jihite]$ sh del.sh a b                                                       number:2scname:del.shdel.shab01argume:a Bab-----------------AB-----------------------------ab-----------------a bshow process id:23582del.sh:line 37:cho: Command not foundshow Precomm stat:127

Problem:

echo #0 #1 can you use $i to express it?

Linux variable $#,[email protected], meaning of $0,$1,$2,$*,$$,$?

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.