I have a stupid question. Can I get the bash location, such as "/bin/bash" or "/usr/bin/bash", before I execute the script?

I tried to do this:

#!`which bash`

Or this similar thing:

#!"${which bash}"

I assumed that the terminal I am running the script on would already have the location of bash stored, and I could leverage this for my script execution. However, this did not seem to be the case.

I do not know if that is possible, but it is worth trying and asking.

Thank you for your time!

  • 2
    Please edit your question to explain the actual problem you are trying to solve - what does "leverage this for my script execution" mean, exactly? Commented May 29 at 10:39

3 Answers 3


You can use /usr/bin/env for this:

#! /usr/bin/env bash

This will run the script using the bash executable found first in the list of directories in your $PATH. This is often used for Python scripts, which typically have a shebang line of:

#! /usr/bin/env python3
  • Your solution is exciting, but I was looking for something in Bash. I was curious if I could do something similar directly in Bash without using another programming language. Commented May 29 at 8:50
  • 11
    @iliealexandru the shebang is read by the kernel before any language comes into play, so no, bash or any other programming language isn't relevant in shebang lines.
    – muru
    Commented May 29 at 9:03
  • 5
    @iliealexandru I'm not getting it, what do you mean by "directly in bash"? Within the script? That's obviously impossible cause you'd be asking a script being run in Bash to retrieve Bash's path, which obviously must have happened already; or are you asking how to determine Bash's path from outside the script before running it? If so just bash script.sh will run the script based on the location of Bash as retrieved from PATH, however please clarify this in your question.
    – kos
    Commented May 29 at 9:17
  • 1
    @iliealexandru env is not "another programming language". It is the customary tool for this. That said, for bash, I doubt the portability of /usr/bin/env is better than of /bin/bash, so I'd probably avoid it for bash.
    – marcelm
    Commented May 30 at 8:48
  • 1
    @iliealexandru : you are asking a XYProblem: you need to execute bash wherever it can be found, but you are asking about how to do that within bash (which is by definition not possible: how would you launch that bash if you don't know its path?). The answer here is the correct answer to your problem, giving the proper way to do what you need, instead of trying to follow the method you would like to be used. Commented May 30 at 15:47

As workaround You can start the script with just a ':' It will run using current shell. Then inside the script you can check /proc/$$/exe to see if is the wanted shell. Example:

ls -l /proc/$$/exe

which bash works for me (Fedora + cinnamon)

You can get the "official" shells called bash, which are the ones which can be invoked via an executable script with a shebang (#!/bin/bash etc at the top).

$ cat /etc/shells | grep bash

Which doesn't answer the question because there are two of them!

Also echo $BASH gives /usr/bin/bash but again, I don;t know how it gets set.

Just checked this answer is also valid on Ubuntu 24.04 LTS server plus xfce desktop. Except /etc/shells also contains rbash.

  • /etc/shells doesn't need to have any relation to reality. It's merely a list of paths permitted as login shells (for example, for changing shells via chsh). You can put any path in it.
    – muru
    Commented Jun 3 at 8:31
  • @muru it's writeable only by root. So one is entitled to expect it to be competently maintained.
    – nigel222
    Commented Jun 5 at 8:26
  • Maybe, but it is entirely possible and even likely for cruft to get added to it
    – muru
    Commented Jun 5 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .