Shell(五)

shell 的變量

#!/bin/bash
# ex9.sh

# Variables: assignment and substitution

a=375
hello=$a
#   ^ ^

#-------------------------------------------------------------------------
# No space permitted on either side of = sign when initializing variables.
# What happens if there is a space?

#  "VARIABLE =value"
#           ^
#% Script tries to run "VARIABLE" command with one argument, "=value".

#  "VARIABLE= value"
#            ^
#% Script tries to run "value" command with
#+ the environmental variable "VARIABLE" set to "".
#-------------------------------------------------------------------------


echo hello    # hello
# Not a variable reference, just the string "hello" ...

echo $hello   # 375
#    ^          This *is* a variable reference.
echo ${hello} # 375
#               Likewise a variable reference, as above.

# Quoting . . .
echo "$hello"    # 375
echo "${hello}"  # 375

echo

hello="A B  C   D"
echo $hello   # A B C D
echo "$hello" # A B  C   D
# As we see, echo $hello   and   echo "$hello"   give different results.
# =======================================
# Quoting a variable preserves whitespace.
# =======================================

echo

echo '$hello'  # $hello
#    ^      ^
#  Variable referencing disabled (escaped) by single quotes,
#+ which causes the "$" to be interpreted literally.

# Notice the effect of different types of quoting.


hello=    # Setting it to a null value.
echo "\$hello (null value) = $hello"      # $hello (null value) =
#  Note that setting a variable to a null value is not the same as
#+ unsetting it, although the end result is the same (see below).

# --------------------------------------------------------------

#  It is permissible to set multiple variables on the same line,
#+ if separated by white space.
#  Caution, this may reduce legibility, and may not be portable.

var1=21  var2=22  var3=$V3
echo
echo "var1=$var1   var2=$var2   var3=$var3"

# May cause problems with legacy versions of "sh" . . .

# --------------------------------------------------------------

echo; echo

numbers="one two three"
#           ^   ^
other_numbers="1 2 3"
#               ^ ^
#  If there is whitespace embedded within a variable,
#+ then quotes are necessary.
#  other_numbers=1 2 3                  # Gives an error message.
echo "numbers = $numbers"
echo "other_numbers = $other_numbers"   # other_numbers = 1 2 3
#  Escaping the whitespace also works.
mixed_bag=2\ ---\ Whatever
#           ^    ^ Space after escape (\).

echo "$mixed_bag"         # 2 --- Whatever

echo; echo

echo "uninitialized_variable = $uninitialized_variable"
# Uninitialized variable has null value (no value at all!).
uninitialized_variable=   #  Declaring, but not initializing it --
                          #+ same as setting it to a null value, as above.
echo "uninitialized_variable = $uninitialized_variable"
                          # It still has a null value.

uninitialized_variable=23       # Set it.
unset uninitialized_variable    # Unset it.
echo "uninitialized_variable = $uninitialized_variable"
                                # uninitialized_variable =
                                # It still has a null value.
echo

exit 0

對比區(qū)別:

newer@ubuntu:~/script$ a=`ls -l`;echo $a
total 20 -rwxr-xr-x 1 root root 200 Nov 17 17:02 1.sh -rwxrwxr-x 1 newer newer 26 Nov 17 21:13 2.sh -rwxr-xr-x 1 root root 182 Nov 15 05:35 bianliang.sh -rwxr-xr-x 1 root root 112 Nov 14 17:12 case.sh -rwxr-xr-x 1 root root 2093 Nov 15 05:50 clean.sh
newer@ubuntu:~/script$ a=`echo hello`;echo "$a"
hello
newer@ubuntu:~/script$ a=`ls -l`;echo "$a"
total 20
-rwxr-xr-x 1 root  root   200 Nov 17 17:02 1.sh
-rwxrwxr-x 1 newer newer   26 Nov 17 21:13 2.sh
-rwxr-xr-x 1 root  root   182 Nov 15 05:35 bianliang.sh
-rwxr-xr-x 1 root  root   112 Nov 14 17:12 case.sh
-rwxr-xr-x 1 root  root  2093 Nov 15 05:50 clean.sh

除了反引號之外俗批,還可以使用()對變量進行賦值。

newer@ubuntu:~/script$ arch=$(uname -a);echo $arch
Linux ubuntu 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22 15:32:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

判斷變量是否被初始化

if [ -z "$unassigned" ]
then
  echo "\$unassigned is NULL."
fi     # $unassigned is NULL.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末磷仰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌宾尚,老刑警劉巖懦傍,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件雹舀,死亡現(xiàn)場離奇詭異,居然都是意外死亡粗俱,警方通過查閱死者的電腦和手機说榆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寸认,“玉大人签财,你說我怎么就攤上這事∑” “怎么了唱蒸?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長灸叼。 經(jīng)常有香客問我神汹,道長庆捺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任屁魏,我火速辦了婚禮疼燥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蚁堤。我一直安慰自己醉者,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布披诗。 她就那樣靜靜地躺著撬即,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呈队。 梳的紋絲不亂的頭發(fā)上剥槐,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機與錄音宪摧,去河邊找鬼粒竖。 笑死,一個胖子當(dāng)著我的面吹牛几于,可吹牛的內(nèi)容都是我干的蕊苗。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼沿彭,長吁一口氣:“原來是場噩夢啊……” “哼朽砰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起喉刘,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤瞧柔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后睦裳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體造锅,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年廉邑,在試婚紗的時候發(fā)現(xiàn)自己被綠了哥蔚。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡鬓催,死狀恐怖肺素,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情宇驾,我是刑警寧澤倍靡,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站课舍,受9級特大地震影響塌西,放射性物質(zhì)發(fā)生泄漏他挎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一捡需、第九天 我趴在偏房一處隱蔽的房頂上張望办桨。 院中可真熱鬧,春花似錦站辉、人聲如沸呢撞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽殊霞。三九已至,卻和暖如春汰蓉,著一層夾襖步出監(jiān)牢的瞬間绷蹲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工顾孽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留祝钢,地道東北人。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓若厚,卻偏偏與公主長得像拦英,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子盹沈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容

  • Shell 流程控制 和Java龄章、PHP等語言不一樣,sh的流程控制不可為空乞封,如(以下為PHP流程控制寫法): 在...
    yyshang閱讀 260評論 0 0
  • 概述 首先,咱們來了解一下岗憋,什么是Shell肃晚。操作系統(tǒng)內(nèi)核給我們提供了各種接口,同時也提供了各種用戶層的庫仔戈,理論上...
    keysaim閱讀 1,381評論 0 0
  • 因為有人牽掛关串,是你的溫馨,因為有人牽掛监徘,是你的幸福晋修。因為有人牽掛,你才會活的自在凰盔,活的瀟灑墓卦,活得那么自以為是。不是...
    戀寶貝閱讀 150評論 0 0
  • 閱讀時間:40分鐘 《高效閱讀法》第4部分:如何做杠桿筆記 讀后感:靠記筆記來留下記錄户敬,并實際運用看看吧落剪!把筆記的...
    windysu閱讀 279評論 0 2
  • 親愛的老爸: 今天過得好嗎睁本?想我了不? 今早起來就困困的忠怖,不知道為什么呢堰。其實昨天睡得挺早的呀,奇怪了凡泣。 今天跟陸姐...
    老爸我很想你閱讀 107評論 0 1