2

I have 2 questions:

  1. I have a script buildllt which uses #!/bin/sh. So, if I run the script using: /bin/bash buildllt. What shell interpreter will the OS use? sh or bash

  2. If after running the script using default shell sh, can we export SHELL env variable as export SHELL = /bin/bash? Will the next statements be interpreted using a different shell?

1
  • Related, you should use #!/usr/bin/env sh, #!/usr/bin/env bash, #!/usr/bin/env python, #!/usr/bin/env python2, etc. Commented Jan 2, 2018 at 11:28

3 Answers 3

3
  1. If you run the script using /bin/bash buildllt then the #! line will be ignored and bash will be used.

  2. No. SHELL is set by a shell as the name of the login shell, it is not read. Even if it was read, exporting it would not affect the parent process. If you want to run an interactive bash session then just type bash or even exec bash to replace your previous sh program.

Sign up to request clarification or add additional context in comments.

Comments

1

The answer to your questions:

  1. If you run an interpreter explicitly, then the shebang will not be taken into account. So /bin/bash buildllt will be run by /bin/bash regardless of the shebang.
  2. The SHELL environment variable is set to the preferred shell that is indicated in the /etc/passwd file (see here). This should be the shell invoked when the user logs in, but will not be used to execute any scripts. The shebang is used for that.

Comments

1

I have a script buildllt which uses #!/bin/sh. So, if I run the script using /bin/bash buildllt. What shell interpreter will the OS use? sh or bash

Of course /bin/bash. The shebang is for the OS to determine which interpreter to use when the script file is executed directly, i.e. when you run ./buildllt. The operating system will read the first line and choose a interpreter for the script. If you invoke bash buildllt then the OS simply executes bash and supply the script as an argument, then bash will ignore the shebang line (because it starts with a hash which is considered as comment).

See more about shebang on Wikipedia

If after running the script using default shell sh, can we export SHELL env variable as export SHELL = /bin/bash

Yes, but it won't have any effect. The SHELL environment variable is to determine which shell to be used as a user's login shell. It's specified in /etc/passwd and is only effective with shell login (like su user or when you log in from a terminal, no effect when logging in with a desktop environment like GNOME). Even if you change it, later scripts won't be affected. They are still run with the interpreter specified by their shebang lines. You surely don't want a Python script #!/usr/bin/python3 to be run by bash, right?

BTW, as tripleee pointed out, export does not accept spaces around the equal sign. You need to write this to make it work.

export SHELL=/bin/bash

2 Comments

Maybe also point out the syntax error in the export -- it won't work with whitespace around the equals sign.
@tripleee Good spot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.