1. ( ~ )
等於 $HOME,如果將 ( ~ ) quoted 起來,則 ( ~ ) 不會 expansion,如下範例紅色部分
範例:
#!/bin/sh
echo "1. '~' is \$HOME "
echo \$HOME = $HOME
echo \~ = ~
echo \"~\" = "~"
結果:
1. '~' is $HOME$HOME = /home/william
~ = /home/william
"~" = ~
2. ( ~login_name)
如果在 ( ~ ) 後面接上一個 login name,則表示 login name 的 home 目錄,如果此
login name 不存在於系統,則不做 expansion,如下範例紅色部分
範例:
#!/bin/sh
echo "2. '~' is followed by a login name"
echo \~william = ~william
echo \~neo = ~neo
echo \~tom = ~tom
結果:
2. '~' is followed by a login name
~william = /home/william
~neo = /home/neo
~tom = ~tom
3. ( ~+ )
等於 $PWD
範例:
#!/bin/sh
echo "3. '~+' is \$PWD"
echo \$PWD = $PWD
echo \~+ = ~+
3. '~+' is $PWD
$PWD = /home/william/test/tilde_expansion
~+ = /home/william/test/tilde_expansion
等於 $OLDPWD,如果 $OLDPWD 是空的,則不做 expansion,如下範例紅色部分
範例:
#!/bin/sh
echo "4. '~-' is \$OLDPWD"
echo \$OLDPWD = $OLDPWD
echo \~- = ~-
echo ""
echo "Change to /home and then change back"
cd /home
cd - > /dev/null
echo \$OLDPWD = $OLDPWD
echo \~- = ~-
結果:
4. '~-' is $OLDPWD$OLDPWD =
~- = ~-
Change to /home and then change back
$OLDPWD = /home
~- = /home
沒有留言:
張貼留言