bash/ksh/scripting eval subshell quotes

 ̄綄美尐妖づ 提交于 2019-12-11 04:48:29

问题


I'm using ksh and having some trouble. Why does this code not run?

[root]$ CMD="ls -ltr"
[root]$ eval "W=$( $CMD )"
[root]$ ksh: ls -ltr:  not found.
[root]$ echo $W 

But this works fine:

[root]$ CMD="ls -ltr"
[root]$ eval 'W=$('$CMD')'
[root]$ echo $W 

回答1:


You need to escape the $(...) with a backslash to prevent it from being evaluated by the outside shell. The $(...) needs be preserved as is until it is handed off to the eval:

$ CMD="ls -ltr"
$ eval "W=\$( $CMD )"
$ echo $W
total 197092 srwxr-xr-x 1 root root...



回答2:


ksh is expanding the $CMD in the first example as a single positional argument whose value is "ls -ltr" (note the space is included. You want it expanded to two arguments: "ls" (the command name) and "-ltr" (the options). The later example cases this expansion because the expansion is in the script and then passed to the sub-shell.

If you were writing a C program, the first example gives you argc = 1 with argv[0] = "ls -ltr" and the second gives you argc = 2 with argv[0] = "ls" and argv[1] = "-ltr". (If that example helps any.)



来源:https://stackoverflow.com/questions/2760744/bash-ksh-scripting-eval-subshell-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!