天天看點

Shell學習筆記---變量的指派與替換

聲明:本文僅作學習研究使用,多數語句都是為了介紹文法而構造的。 

建議讀者先将以下指令分别執行後再整合成腳本,這樣會對變量的整個指派過程了解更透徹。 

#!/bin/bash

#filename:prameter_practise1

#datetime:2010_12_24 08:25

#discription:practise of using prameters 

a=375

hello=$a 

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

# 強烈注意, 在指派的的時候, 等号前後一定不要有空格.考慮如果出現空格會怎麼樣?

echo hello # 沒有變量引用, 隻是個hello字元串.

echo $hello

echo ${hello}    // 同上.

echo "$hello"

echo "${hello}"

echo

hello="a b c d"

echo $hello # a b c d

echo "$hello" # a b c d 

# 就象你看到的 echo $hello 和 echo "$hello" 将給出不同的結果.

echo '$hello' # $hello 

# 全引用的作用将會導緻"$"被解釋為單獨的字元,而不是變量字首.

hello= # 設定為空值.

echo "\$hello (null value) = $hello" 

# 注意設定一個變量為null, 與unset這個變量, 并不是一回事,雖然最終的結果相同(具體見下邊).

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

echo; echo

numbers="one two three"

other_numbers="1 2 3" 

# 如果在變量值中存在空白, 那麼就必須在指派時加上引用.

# other_numbers=1 2 3 # 将給出一個錯誤消息.

echo "numbers = $numbers"

echo "other_numbers = $other_numbers" # other_numbers = 1 2 3

echo "uninitialized_variable = $uninitialized_variable" 

# uninitialized變量為null(就是沒有值).

uninitialized_variable= # 聲明, 但是沒有初始化這個變量,

#其實和前邊設定為空值的作用是一樣的.

# 還是一個空值.

uninitialized_variable=23 # 指派.

unset uninitialized_variable # unset這個變量.

exit 0 

如有錯誤,歡迎指正

郵箱:[email protected] 

作者:czmmiao 原文位址:http://czmmiao.iteye.com/blog/911374