參數的assign方式? - Linux

Table of Contents

/ 過參數 但好像沒有關於這問題
比如以下script:
swap()
{
tmp=$1
v2= $2
$1=${!v2}
$2=${!tmp}
}

a=5
b=1
swap a b
echo "after swap:a=$a, b=$b"

會出現command not found的message

function好像真的還蠻不好用的 不像一般程式語言可以改argument
有沒有大大有方式可以改argument的value而不經由修改全域變數的方式進行?

--

All Comments

Barb Cronin avatarBarb Cronin2013-12-16
v2= 後面不能有空白
Frederic avatarFrederic2013-12-17
$1不是這樣賦值 要用set -- ${!v2} ${!tmp}
Freda avatarFreda2013-12-21
另外每次call函數有自己的$@. 在裡面改也不影響全域
Doris avatarDoris2013-12-21
因此swap之後的echo經由上面的方法不會有變化?
Mary avatarMary2013-12-26
eval a="$b" b="$a"
Mary avatarMary2013-12-30
eval a=\"$b\" b=\"$a\" #改一下