1

Does the sh linux command use the users current shell, even if its not bash? For example, let's say the user bob is running csh. If I create a script with functions and csh and try and execute it with:

sh cshtestscript

Will this run with csh?

Then assume another use joe is using bash. If I create another script with functions and bash script and try and execute it with:

sh bashtestscript

Will this run with bash?

Justin
  • 5,328
  • 19
  • 64
  • 84

1 Answers1

4

sh is just a standard executable, which is always a specific shell. There is no magic to detect which shell to use.

% whereis sh
sh: /bin/sh
% ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 29 11:53 /bin/sh -> dash

On Ubuntu systems, /bin/sh is (by default) a symlink to dash, which is a minimal POSIX shell intended for non-interative use. If you run sh cshtestscript on an Ubuntu system, it will try to run the script with dash.

The correct way to handle this is to add a shebang to the script which indicates which shell to use, make the script executable, and always execute it as ./cshtestscript.

#!/bin/csh
mgorven
  • 30,615
  • 7
  • 79
  • 122
  • Yeah, basically I am looking for a way to run these scripts without actually creating files. So something like: `sh echo "#!/bin/bash free -m"`. Would that work? – Justin Jun 21 '12 at 06:49
  • @Justin Just use `bash -c 'free -m'` – mgorven Jun 21 '12 at 07:02