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

Vol*_*lyy 2 unix shell aix ksh

在AIX上运行时,以下两个命令之间有什么区别?

/bin/sh 'ls -l -R'
/bin/sh -c 'ls -l -R'
Run Code Online (Sandbox Code Playgroud)

Phi*_*oss 10

在AIX上,/bin/sh默认为Korn Shell(ksh).

/bin/sh 'ls -l -R'
Run Code Online (Sandbox Code Playgroud)

使用ksh,它运行shell,告诉它执行命名的脚本ls -l -R(在当前目录或路径上).如果找不到具有此名称的脚本,则ksh将该参数视为命令并运行它.

请注意,使用bash时,如果找不到脚本,则会导致错误.

/bin/sh -c 'ls -l -R'
Run Code Online (Sandbox Code Playgroud)

这将启动shell的新实例,告诉它运行该命令ls -l -R.


rei*_*ost 5

(这是对Phil Ross的回应,但我需要格式化:)

使用我的一个Linux帐户:

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> 
Run Code Online (Sandbox Code Playgroud)

使用我的一个Cygwin安装:

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> 
Run Code Online (Sandbox Code Playgroud)

/bin/sh可能会有所不同,但这似乎是适当的行为:

  • 向/ bin/sh提供文件名参数会将其解释为shell脚本的文件名并尝试执行它
  • 向-c提供一个参数会将该参数解释为/ bin/sh命令行并尝试执行该操作

有关详细信息,请键入

man sh
Run Code Online (Sandbox Code Playgroud)