linux中shell的變數用法

2021-09-05 11:58:00 字數 3527 閱讀 8777

一、環境變數

環境變數也可叫全域性變數,可以在建立他們的shell及派生出的子shell中使用(無需定義,直接可以使用,如:$uid)

相關命令:

set :輸出所有變數

env:只顯示全域性變數

declare:輸出所有變數,函式,整數等

二、普通變數

普通變數賦值

變數名=value

變數名=『value』

變數名=「value」

注意:建議沒有特別要求時,字串都加雙引號,需要原樣輸出就加單引號

[root@localhost ~]# a=hello

[root@localhost ~]# echo $a

hello

[root@localhost ~]# b='hello'

[root@localhost ~]# echo $b

hello

[root@localhost ~]# c="hello"

[root@localhost ~]# echo $c

hello

[root@localhost ~]# a=westos-$a

[root@localhost ~]# echo $a

westos-hello

[root@localhost ~]# b='westos-$a'         ##不轉譯變數,按原樣輸出

[root@localhost ~]# echo $b

westos-$a

[root@localhost ~]# c="westos-$a"         ##轉移變數

[root@localhost ~]# echo $c

westos-westos-hello

[root@localhost ~]# a=westos hello        ##定義變數有空格時,要加「」

bash: hello: command not found...

[root@localhost ~]# a="westos hello"

[root@localhost ~]# echo $a

westos hello

命令結果作為內容賦值

變數名=命令

b=ls或 b=$(ls)

[root@localhost mnt]# cmd=`ls -l`

[root@localhost mnt]# echo $cmd

total 8 -rwxr-xr-x. 1 root root 492 dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 dec 22 10:40 westos.sh

[root@localhost mnt]# cmd=$(ls -l)

[root@localhost mnt]# echo $cmd

total 8 -rwxr-xr-x. 1 root root 492 dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 dec 22 10:40 westos.sh

三、特殊變數

$0:獲取shell指令碼檔名,如果執行時包含路徑,則輸出指令碼路徑

$n(>0):獲取指令碼的第n個引數

$#:獲取指令碼後引數的總個數

$*:獲取所有引數

$@:獲取所有引數

$?:獲取上一條命令執行狀態的返回值,非0為失敗

$$:獲取當前shell程序號

$0[root@localhost mnt]# cat westos.sh

#!/bin/bash

#echo $0

[root@localhost mnt]# sh westos.sh

westos.sh

[root@localhost mnt]# /mnt/westos.sh

/mnt/westos.sh

$n[root@localhost mnt]# cat westos.sh

#!/bin/bash

echo $1 $2

[root@localhost mnt]# sh westos.sh hello westos

hello westos

[root@localhost mnt]# sh westos.sh hello redhat

hello redhat

[root@localhost mnt]# echo \$ > westos.sh

[root@localhost mnt]# cat westos.sh

$1 $2 $3 $4 $5 $6 $7 $8 $9 $10

[root@localhost mnt]# sh westos.sh 

a b c d e f g h i a0

[root@localhost mnt]# cat westos.sh

$1 $2 $3 $4 $5 $6 $7 $8 $9 $

[root@localhost mnt]# sh westos.sh 

a b c d e f g h i j

[root@localhost mnt]# cat westos.sh

echo $#

[root@localhost mnt]# sh westos.sh

100[root@localhost mnt]# ls /etc/passwd

/etc/passwd

[root@localhost mnt]# echo $?

0[root@localhost mnt]# ls /etc/password

ls: cannot access /etc/password: no such file or directory

[root@localhost mnt]# echo $?2$$

[root@localhost mnt]# echo $$

2795

[root@localhost mnt]# ps

pid tty          time cmd

2795 pts/2    00:00:00 bash

4277 pts/2    00:00:00 ps

四、read用法

[root@localhost mnt]# read str

westos hello

[root@localhost mnt]# echo $str

westos hello

[root@localhost mnt]# read -p "請輸入乙個整數:" i

請輸入乙個整數:10

[root@localhost mnt]# echo $i

10五、練習

打包日誌:(打包成log_yyyy-mm-dd.tar.gz的格式 )

[root@localhost mnt]# tar zcf log_$(date +%f).tar.gz /var/log/

tar: removing leading `/' from member names

[root@localhost mnt]# ls

log_2018-12-22.tar.gz  test.sh  westos.sh

linux中shell的變數型別

shell中的變數型別有 本地變數 環境變數 位置變數 標準變數 特殊變數。2.1.本地變數在shell指令碼的生存週期中使用的變數 也就是登入乙個shell到退出,這個shell為生存週期 設定乙個本地變數格式為 變數 名 變數,顯示乙個變數 echo 也可以不要大括號,顯示本地所有變數 set就...

Linux程式設計 Shell中的變數

系統啟動後會產生許多環境變數,使用者可以使用 set 命令檢視這些環境變數 家目錄位置變數 home 系統語言變數 lang 臨時修改系統語言可以重新指定此變數的值 lang zh cn.utf 8 互動程式變數 shell 命令搜尋路徑變數 path 主提示符變數 ps1 檢視當前使用的主提示符表...

linux中shell變數含義

linux中shell變數katex parse error expected eof got at position 1 0,1,2的含 釋 變數說明 shell本身的pid processid shell最後執行的後台process的pid 最後執行的命令的結束 返回值 使用set命令設定的fl...