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?


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.

  • 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 6:49
  • @Justin Just use bash -c 'free -m' – mgorven Jun 21 '12 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.