遇到星號會亂掉,這啥鬼啊 - Linux

Table of Contents

這是我的 shell script
#!/bin/bash
routeStr2=$(route )

echo "================="
echo $(echo $routeStr2 | grep -c "default")
echo $routeStr2

exit 0

因為我想判斷route指令跑出來的東東有多少default指令, 迴圈就跑多少次...
問題來了
如果我把route執行結果直接dump string,
他會是一個字串,中間沒有斷行,
例如
routeStr2
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use
Iface default 172.11.23.1 0.0.0.0 UG 0 0 0 eth0 default 192.168.42.129
0.0.0.0 UG 10 0 0 usb0 10.2.0.0 = download dwhelper examples.desktop font
fontconfig hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img
index.html magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh
VirtualBox VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂
255.255.0.0 U 1 0 0 eth1 140.112.172.0 192.168.42.129 255.255.255.0 UG 0 0 0
usb0 172.11.23.0 = download dwhelper examples.desktop font fontconfig
hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img index.html
magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh VirtualBox
VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂 255.255.254.0 U
1 0 0 eth0 192.168.42.0 = download dwhelper examples.desktop font fontconfig
hs_err_pid27661.log hs_err_pid2938.log hs_err_pid2956.log img index.html
magic.mgc olv-0.log Perforce perforce.desktop proxy.pac test.sh VirtualBox
VMs workspace 公共 圖片 影片 文件 未命名文件 1 桌面 模板 音樂 255.255.255.0 U
1 0 0 usb0

很亂吧, 只要routeStr2裡頭有星字號, bash會自動帶入當下目錄的內容
變成一沱怪東西


後來改為
#!/bin/bash
routeStr2=$(route )

echo "================="
echo $(echo "$routeStr2" | grep -c "default")
echo "$routeStr2"

星字號就不會被bash亂轉譯
請問這是為啥呢?

謝謝~




--

All Comments

Jake avatarJake2015-09-17
* 在 echo 內變成是 glob pattern…
Bennie avatarBennie2015-09-20
script內最好的解應該是設GLOBIGNORE了,除非你會要用
到 * 來 match
Elvira avatarElvira2015-09-23
是的,* 就是萬用字元,除了 . 開頭的全部都 match
Thomas avatarThomas2015-09-24
當然,如果是 a* 會 match a.out
Adele avatarAdele2015-09-24
某些沒有 ls 的系統,ls 其實 echo * (busybox)
Catherine avatarCatherine2015-09-24
解決方式是把 echo $routeStr2 改成 echo "$routeStr2"
Ina avatarIna2015-09-26
pttnews 這個是官方帳號還是?