这是我的代码,我尝试打印字符串的每个字符:
#!/bin/bash
seq="2 + 3 * 4"
for i in $seq ; do
echo "printing $i"
done
但是当我到达" *"字符串$ seq的字符程序打印文件所在文件夹中的所有文件。以下是上述代码的示例输出:
printing 2
printing +
printing 3
printing file
printing fileHandle.pl
printing indentation.sh
printing line_no.sh
如何通过上面的循环打印*字符?
答案 0 :(得分:4)
您可以为该特定会话禁用通配:
shopt -s -o noglob
seq="2 + 3 * 4"
for i in $seq ; do
echo "printing $i"
done
shopt -u -o noglob ## Optionally enable it back
输出:
printing 2
printing +
printing 3
printing *
printing 4
答案 1 :(得分:3)
您可以使用read -a
:
#!/bin/bash
seq="2 + 3 * 4"
read -ra arr <<< "$seq"
for i in "${arr[@]}"
do
echo "$i"
done
答案 2 :(得分:1)
这很愚蠢,但也有效:
seq="2 + 3 * 4"
while read -rd ' ' token; do
echo "printing $token"
done <<< "$seq "