我设法在我正在处理的init脚本中跟踪完成了一个奇怪的问题.我在以下示例中简化了问题:
> set -x # <--- Make Bash show the commands it runs
> cmd="echo \"hello this is a test\""
+ cmd='echo "hello this is a test"'
> $cmd
+ echo '"hello' this is a 'test"' # <--- Where have the single quotes come from?
"hello this is a test"
Run Code Online (Sandbox Code Playgroud)
为什么bash将这些额外的单引号插入到执行的命令中?
额外的引号在上面的例子中没有引起任何问题,但它们真的让我头疼.
对于好奇,实际的问题代码是:
cmd="start-stop-daemon --start $DAEMON_OPTS \
--quiet \
--oknodo \
--background \
--make-pidfile \
$* \
--pidfile $CELERYD_PID_FILE
--exec /bin/su -- -c \"$CELERYD $CELERYD_OPTS\" - $CELERYD_USER"
Run Code Online (Sandbox Code Playgroud)
产生这个:
start-stop-daemon --start …Run Code Online (Sandbox Code Playgroud) 谁能告诉我这里的最大区别是什么,为什么后者不起作用?
test="ls -l"
Run Code Online (Sandbox Code Playgroud)
现在两者都可以正常工作:
eval $test
echo `$test`
Run Code Online (Sandbox Code Playgroud)
但在这种情况下:
test="ls -l >> test.log"
eval $test
echo `$test`
Run Code Online (Sandbox Code Playgroud)
后者将不起作用。这是为什么?我知道 eval 只是执行一个脚本,而撇号正在执行它并将结果作为字符串返回。是什么使得无法>>在命令中使用或类似的东西来执行?也许有没有办法让它与撇号一起工作,而我做错了什么?