这是我感兴趣的单行代码(尤其是在变量out
中(:
find ~ | head -3 | while read f; do out=$(dirname ${f}); echo ${out}; done
我需要在将它管道到另一个程序之前回显这个单行,但它失败了:
echo "find ~ | head -3 | while read f; do out=$(dirname ${f}); echo ${out}; done"
并显示以下消息:
dirname: missing operand
Try `dirname --help' for more information.
find ~ | head -3 | while read f; do out=; echo ; done
所以我使用单引号和双引号:
echo "find ~ | head -3 | while read f; do out=$(dirname "'${f}'"); echo "'${out}'"; done"
返回没有错误:
find ~ | head -3 | while read f; do out=.; echo ${out}; done
但$(dirname ${f})
并没有像现在这样得到回应。
知道怎么做吗?
为了防止替换,请使用单引号或转义$
:
echo 'find ~ | head -3 | while read f; do out=$(dirname ${f}); echo ${out}; done'
echo "find ~ | head -3 | while read f; do out=$(dirname ${f}); echo ${out}; done"
使用变量来存储命令并回显变量:
cmd='find ~ | head -3 | while read f; do out=$(dirname ${f}); echo ${out}; done'
echo $cmd
我不知道为什么您只需要使用echo
进行打印。但是,如果您只想以这种方式,请参阅以下内容:
echo `find ~ | head -3 | while read f; do out=$(dirname ${f}); echo ${out}; done`