1

I was working on this tutorial here and I want to know the path so I can put it in my shell scripts on the top line as is good practice.

I was told this is not accurate:

echo "$SHELL"

What is the correct way to determine the path to the interpreter for shell scripts?

This is a shared hosting account and it is not obvious where it is located.

This is a follow up to a similar but not duplicate here.

Community
  • 1
  • 1
cade galt
  • 3,843
  • 8
  • 32
  • 48
  • So you want to find out what your default login shell is, and use that for any script that you write, is that correct? If so, you could find your default shell by looking at your username in /etc/passwd, it'll be the last field. – Eric Renouf Aug 15 '15 at 13:53
  • Who told you that your solution is not accurate? Did they say why? Maybe it's not POSIX. Do you consider shells other than `bash`? From `man bash`: "SHELL The full pathname to the shell is kept in this environment variable. If it is not set when the shell starts, bash assigns to it the full pathname of the current user's login shell." – Andras Deak -- Слава Україні Aug 15 '15 at 13:54
  • Possible duplicate of http://stackoverflow.com/questions/11059067/what-is-the-nix-command-to-view-a-users-default-login-shell – Eric Renouf Aug 15 '15 at 13:55
  • 1
    @cadegalt I think that comment means that a script can use a different shell to execute if it has another shebang line. `SHELL` is the currently running shell though, so is what will run any script that does not specifically use another – Eric Renouf Aug 15 '15 at 14:03
  • I'm still not sure about your precise intensions, but you might be just looking for `which bash`. – Andras Deak -- Слава Україні Aug 15 '15 at 14:05
  • @cadegalt Also, you can write the script for any shell you like, regardless of what shell you are currently running, they do not have to match if you don't want them to (which is why you can specify a shebang line, but don't have to). If you want to write a bash script, you can put bash script in the shebang line even if you're running tcsh as your login shell – Eric Renouf Aug 15 '15 at 14:06
  • That might not be all that surprising, at least if we're talking about a simple desktop system. This is more important on servers with multiple users and multiple shells installed. – Andras Deak -- Слава Україні Aug 15 '15 at 14:14
  • Again, it also depends on what you would want to use it for. Furthermore, I'm not completely clear on the subject myself:) Feel free to give an answer yourself as far as I'm concerned. Now it seems to me that `$SHELL` is only set by `bash`, so a user with `tcsh` login shell would have an empty string. If you want to make sure that `bash` runs your executable script, then `which bash` is what you need. And I don't think I know how you can tell the actual interpreter running your shell script... – Andras Deak -- Слава Україні Aug 15 '15 at 14:34
  • 1
    `SHELL` is your preferred (i.e., login) shell, as [specified by POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03). – chepner Aug 15 '15 at 16:18

1 Answers1

2

You can use

which -a bash

to list all paths for the current user that point to a bash program.