Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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