views:

439

answers:

2

What is the difference between the following two commands, when run on AIX?

/bin/sh 'ls -l -R'
/bin/sh -c 'ls -l -R'
+7  A: 

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.

Phil Ross
>/bin/sh 'ls -l -R' total 80 -rw-rw---- 1 user user 39301 Mar 22 18:17 db.trace
Vladimir Bezugliy
I do not have any problems with the command /bin/sh 'ls -l -R'
Vladimir Bezugliy
@Vladimir: Then you have different version of sh. On my system, the above command results in: `/bin/sh: Can't open ls -l -R`. What does `ls -l /bin/sh` show on your system?
Heinzi
>ls -l /bin/sh-r-xr-xr-x 5 bin bin 246558 Sep 20 2008 /bin/sh
Vladimir Bezugliy
@Vladimir What is the result of running `/bin/sh --version`?
Phil Ross
$ /bin/sh --version--version: bad option(s)
Vladimir Bezugliy
It is AIX not Linux.
Vladimir Bezugliy
@Vladimir Since, you are using AIX, `sh` is probably the Korn shell (ksh). I've updated my answer.
Phil Ross
+4  A: 

(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 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
reinierpost