Linux shell中一些引數與變數簡介

2022-09-15 05:39:08 字數 1398 閱讀 9489

linux中shell變數$#,$@,$0,$1,$2,$!,$$,$*,$-,$@......等很多個,很容易記錯,這裡再次整理一下,相關含**釋如下,並附上乙個實踐截圖。

多看幾次,多用幾次,應該就記熟悉了。

變數說明: 

$$ shell本身的pid(processid) 

$! shell最後執行的後台process的pid 

$? 最後執行的命令的結束**(返回值) 

$- 使用set命令設定的flag一覽 

$* 所有引數列表。如"$*"用「"」括起來的情況、以"$1 $2 … $n"的形式輸出所有引數。 

$@ 所有引數列表。如"$@"用「"」括起來的情況、以"$1" "$2" … "$n" 的形式輸出所有引數。 

$# 新增到shell的引數個數 

$0 shell本身的檔名 

$1~$n 

新增到shell的各引數值。$1是第1引數、$2是第2引數…。 

根據上面的解釋,我做了一下實驗,如下:

#!/bin/bash

printf "the complete list \$\$ is %s\n" "$$"

printf "the complete list \$\! is %s\n" "$!"

printf "the complete list \$\- is %s\n" "$-"

printf "the complete list \$\? is %s\n" "$?"

printf "the complete list \$\* is %s\n" "$*"

printf "the complete list \$\@ is %s\n" "$@"

printf "the complete list \$\# is %s\n" "$#"

printf "the complete list \$\0 is %s\n" "$0"

printf "the complete list \$\1 is %s\n" "$1"

printf "the complete list \$\2 is %s\n" "$2"

printf "the complete list \$0,\$1,\$2,\$3,\$4 is %s\n" "$0,$1,$2,$3,$4"

shell執行結果如下:

對著再看幾次,應該就記住了。

也就是說:

$# 是傳給指令碼的引數個數

$0 是指令碼本身的名字

$1是傳遞給該shell指令碼的第乙個引數

$2是傳遞給該shell指令碼的第二個引數

$@ 是傳給指令碼的所有引數的列表

知識庫:

Linux shell中一些引數與變數簡介

linux中shell變數 0,1,2,等很多個,很容易記錯,這裡再次整理一下,相關含 釋如下,並附上乙個實踐截圖。多看幾次,多用幾次,應該就記熟悉了。變數說明 shell本身的pid processid shell最後執行的後台process的pid 最後執行的命令的結束 返回值 使用set命令設...

zen cart中一些常用的後台設定引數

這裡是分類模組的引數設定 lookup constant definitions key show categories box products new title 分類欄 顯示新進商品鏈結 description 在分類邊框中顯示新進商品鏈結 value false group id 19 布局...

PB中一些技巧

取資料視窗中可列新的表名。string ls table ls table dw 1.describe datawindow.updatetable.table 22.取資料視窗物件中列的名稱及型別 string ls cols,ls types intli count,i li count int...