开发者

"/bin/sh 'ls -l -R'" vs "/bin/sh -c 'ls -l -R'"

What is the difference between the following two commands, when run on 开发者_如何学GoAIX?

/bin/sh 'ls -l -R'
/bin/sh -c 'ls -l -R'


On AIX, /bin/sh defaults to the Korn Shell (ksh).

/bin/sh 'ls -l -R'

With ksh, this runs the shell, telling it to execute the script named ls -l -R (in the current directory or on the path). If no script can be found with this name, ksh treats the argument as a command and runs it.

Note that with bash, if the script cannot be found, this would result in an error.

/bin/sh -c 'ls -l -R'

This starts a new instance of the shell, telling it to run the command ls -l -R.


(This is in response to Phil Ross, but I need formatting:)

Using one of my Linux accounts:

sh> /bin/sh --version
GNU bash, version 3.2.39(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
sh>  /bin/sh 'ls -l -R'
/bin/sh: ls -l -R: No such file or directory
sh> /bin/sh ls
/bin/ls: /bin/ls: cannot execute binary file
sh> /bin/sh -c 'ls -l | head -1'
total 147296
sh> 

Using one of my Cygwin installations:

sh> /bin/sh --version
GNU bash, version 3.2.49(23)-release (i686-pc-cygwin)
Copyright (C) 2007 Free Software Foundation, Inc.
sh> /bin/sh 'ls -l -R'
/bin/sh: ls -l -R: No such file or directory
sh> /bin/sh ls
/usr/bin/ls: /usr/bin/ls: cannot execute binary file
sh> /bin/sh -c 'ls -l | head -1'
total 264744
sh> 

Your /bin/sh may vary, but this appears to be the appropriate behaviour:

  • supplying a filename argument to /bin/sh will interpret it as the filename of a shell script and try to execute it
  • supplying -c with an argument will interpret that argument as a /bin/sh command line and try to execute that

For more details, type

man sh
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜