在CentOS系统中,反引号(``)常用于命令替换,但使用不当会导致错误。以下是一些常见错误及其解决方法:
单引号问题:
for test in 'i don't know if this'll work'; do
echo "word:$test"
done
输出:word:i word:don't word:know word:if word:this'll word:work
for test in i don\'t know if this\'ll work; do
echo "word:$test"
done
for test in "i don't know if this'll work"; do
echo "word:$test"
done
空格问题:
for place in new york new hampshire new mexico; do
echo "now going to $place"
done
输出:now going to new now going to york now going to new now going to hampshirenow going to new now going to mexico
for place in "new york" "new hampshire" "new mexico"; do
echo "now going to $place"
done
变量替换问题:
total=`wc -l < a.txt`
sed -n `$((total-4)),$p' a.txt
报错原因:$
在sed中是特殊符号,且$(())部分需要shell解析。sed -n "$((total-4))",$p' a.txt
sed -n `$((total-4))',$p' a.txt
通过合理使用单引号、双引号以及转义字符,可以有效避免反引号在CentOS系统中带来的常见错误。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: dmesg 日志中的驱动信息重要吗