$# and S* in shell script

From here:

$#    Stores the number of command-line arguments that 
      were passed to the shell program.
$?    Stores the exit value of the last command that was 
      executed.
$0    Stores the first word of the entered command (the 
      name of the shell program).
$*    Stores all the arguments that were entered on the
      command line ($1 $2 ...).
"$@"  Stores all the arguments that were entered
      on the command line, individually quoted ("$1" "$2" ...).

So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments. For example, $1 is the first argument and so on. This is useful, if you want to access a specific argument in your script.

As Brian commented, here is a simple example. If you run following command:

./command -yes -no /home/username
  • $# = 3
  • $* = -yes -no /home/username
  • $@ = array: {"-yes", "-no", "/home/username"}
  • $0 = ./command, $1 = -yes etc.

FROM HERE

中文:

$0 程序名字
$n 第n个参数值,n=1..9
$* 所有命令行参数
$@ 所有命令行参数,如果它被包含在引号里,形如”$@”,则每个参数也各自被引号包括
$# 命令行参数个数
$$ 当前进程的进程ID(PID)
$!  最近后台进程的进程ID
$?  最近使用命令的退出状态
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s