linux中shell變量$#,$@,$0,$1,$2的含義解釋


 

常用的shell中帶有$符號的幾個變量的含義:

區別:@ *

  • 相同點:都是引用所有參數
  • 不同點:只有在雙引號中體現出來。假設在腳本運行時寫了三個參數(分別存儲在2 3則"*" 等價於 “3";而“@" 等價於 "1"2"  "$3"(傳遞了三個參數)


變量說明: 
$$ 
Shell本身的PID(ProcessID) 
$! 
Shell最后運行的后台Process的PID 
$? 
最后運行的命令的結束代碼(返回值) 
$- 
使用Set命令設定的Flag一覽 
$* 
所有參數列表。如"$*"用「"」括起來的情況、以"$1 $2 … $n"的形式輸出所有參數。 
$@ 
所有參數列表。如"$@"用「"」括起來的情況、以"$1" "$2" … "$n" 的形式輸出所有參數。 
$# 
添加到Shell的參數個數 
$0 Shell本身的文件名 
$1~$n 
添加到Shell的各參數值。$1是第1參數、$2是第2參數…。 

示例:

1 #!/bin/bash
  2 #
  3 printf  "The complete list is %s\n"  "$$"
  4 printf  "The complete list is %s\n"  "$!"
  5 printf  "The complete list is %s\n"  "$?"
  6 printf  "The complete list is %s\n"  "$*"
  7 printf  "The complete list is %s\n"  "$@"
  8 printf  "The complete list is %s\n"  "$#"
  9 printf  "The complete list is %s\n"  "$0"
10 printf  "The complete list is %s\n"  "$1"
11 printf  "The complete list is %s\n"  "$2
 

結果:

$ bash test .sh 123456 QQ
The complete list  is  22216
The complete list  is
The complete list  is  0
The complete list  is  123456 QQ
The complete list  is  123456
The complete list  is  QQ
The complete list  is  2
The complete list  is  params .sh
The complete list  is  123456
The complete list  is  QQ
 
Have a nice day!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM