2

If the shell script is

#!/bin/sh

But the value of my $SHELL is ksh.

Will it make a difference if I changed my ksh to sh, and then executed the script. ie. will a script have different behaviour depending on what type of shell is executing it.

4

will a script have different behaviour depending on what type of shell is executing it.

  • In the sense that bash script.sh and ksh script.sh are likely to behave differently, yes. Commonly, that difference will be that one of them works and one gives an error, but there are a range of options. Many simple scripts will have the same behaviour on common shells, but more complex scripts are likely to hit one of the many differences between the languages provided by different shells.

  • Will a script behave differently depending on your value of SHELL? Only if the script either invokes $SHELL itself, or tests or otherwise uses its value, directly or indirectly. Ordinary shell scripts generally will not, but they can.

  • Will a script behave differently depending on the parent shell from which it was invoked? Extremely rarely - the script would have to do a fair bit of work to detect that, to the extent that it would almost have to be on purpose.


I think your use case is running ./script.sh, which is a sh script, from your interactive shell, which is ksh. If that's right, we're in the last case above, and the script will almost certainly behave in the same way as if you were using any other shell yourself. The system will always start up a new /bin/sh process and tell it to execute the script.

  • 1
    The only thing I would add is if you execute the script using . script.sh or source script.sh, it will use your current shell, not spawn a new one. Which means that then it will make a difference what shell you are using. – daniel kullmann Oct 20 '14 at 7:58
0

No, it doesn't make a difference. #!/usr/bin/sh means "use /usr/bin/sh to interpret this script so the value of your $SHELL makes no difference.

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.