shell学习8:$#,$$,$?的作用

$#:看脚本的输入参数个数是多少;

$$:脚本执行的进程号;

$?:上个命令的退出状态,在shell的图形界面zenity里面用的比较多。 

$#测试代码:

  1 #!/bin/bash
  2 #
  3 echo "test : \$#"
  4 echo $#
  5 echo "$#"
  6 exit 0

测试结果:

gyz@debian:~/shelltest$ ./aa_test.sh 
test : $#
0
0
gyz@debian:~/shelltest$ ./aa_test.sh 2
test : $#
1
1
gyz@debian:~/shelltest$ ./aa_test.sh a b c
test : $#
3
3

$$测试代码:

  1 #!/bin/bash
  2 #
  3 echo "test : \$\$"
  4 echo $$
  5 echo "$$"
  6 exit 0

代码测试结果:

gyz@debian:~/shelltest$ ./aa_test.sh a b c
test : $$
2089
2089
gyz@debian:~/shelltest$ ./aa_test.sh 
test : $$
2090
2090

$?测试代码:

1 #!/bin/bash
  2 #
  3 echo "test : \$?"
  4 
  5 function fun_t()
  6 {
  7     return 1
  8 }
  9 function fun_f()
 10 {
 11     return 0
 12 }
 13 
 14 if [ $1 == 10 ]
 15 then
 16     fun_t
 17 else
 18     fun_f
 19 fi

测试结果:

gyz@debian:~/shelltest$ ./aa_test.sh 10
test : $?
gyz@debian:~/shelltest$ echo $?
1
gyz@debian:~/shelltest$ ./aa_test.sh 9
test : $?
gyz@debian:~/shelltest$ echo $?
0

参考:http://c.biancheng.net/view/808.html

你可能感兴趣的:(shell学习之路,shell,$$,$#,$?)