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

對(duì)比區(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

除了反引號(hào)之外,還可以使用()對(duì)變量進(jìn)行賦值。

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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Shell 流程控制 和Java、PHP等語言不一樣,sh的流程控制不可為空,如(以下為PHP流程控制寫法): 在...
    yyshang閱讀 309評(píng)論 0 0
  • 概述 首先,咱們來了解一下,什么是Shell。操作系統(tǒng)內(nèi)核給我們提供了各種接口,同時(shí)也提供了各種用戶層的庫(kù),理論上...
    keysaim閱讀 1,523評(píng)論 0 0
  • 因?yàn)橛腥藸繏欤悄愕臏剀?,因?yàn)橛腥藸繏?,是你的幸福。因?yàn)橛腥藸繏?,你才?huì)活的自在,活的瀟灑,活得那么自以為是。不是...
    戀寶貝閱讀 172評(píng)論 0 0
  • 閱讀時(shí)間:40分鐘 《高效閱讀法》第4部分:如何做杠桿筆記 讀后感:靠記筆記來留下記錄,并實(shí)際運(yùn)用看看吧!把筆記的...
    windysu閱讀 362評(píng)論 0 2
  • 親愛的老爸: 今天過得好嗎?想我了不? 今早起來就困困的,不知道為什么。其實(shí)昨天睡得挺早的呀,奇怪了。 今天跟陸姐...
    老爸我很想你閱讀 139評(píng)論 0 1

友情鏈接更多精彩內(nèi)容