0

What is the benefit of executing bash command? In my Terminal Window, nothing visible to the eye happens. I noticed that the $SHLVL gets incremented, but beside that I wouldn't know that bash was executed. Additionally running bash --help doesn't really tell much. I know that bash is one of the shells available, but if you are already using Bourne Again shell, I see no benefits of nesting it. In what scenarios should I execute bash?

closed as unclear what you're asking by Jeff Schaller, sam, Satō Katsura, GAD3R, Wildcard Jan 31 '17 at 23:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    bash creates a new shell as a child. You can make arbitrary changes to your environment (example, change $IFS or unset $PATH), and when you exit that child shell those changes disappear with it. – glenn jackman Jan 31 '17 at 17:20
  • If you see no change in a terminal window, chances are that you're already running it. – roaima Jan 31 '17 at 17:35
  • 1
    what are you comparing against? (not executing the bash command is one in a long list of things that aren't "executing the bash command") – Jeff Schaller Jan 31 '17 at 18:06
3

Running bash by itself if you're already running a bash shell only really has one plausible benefit - the new shell will have a "clean" environment (notwithstanding any exports).

However, if you're currently running any other shell and want to invoke a bash script, and its permissions are not set +x (i .e. executable) with a proper shebang line, you would use bash /path/to/script.sh to ensure it runs with the proper shell.

  • I shall clarify the wording. – DopeGhoti Jan 31 '17 at 17:44
3

Starting a new shell session with bash (or whatever shell you start) sets up a new environment. It will inherit any environment variables1 from the parent shell session. Once the new session exits, its environment is destroyed.

Apart from using it as the explicit interpreter to execute scripts (to bypass the #!-line in the script itself), or for executing specific commands in a bash environment with bash -c, starting an interactive shell with just bash may be useful for testing things.

I use it all the time since my login shell is ksh93 rather than bash, and so many questions here seems to be about bash. Once I've tested whatever it is I wanted to test, I just exit (or Ctrl+D) and get my trusty ksh93 session back without any pollution from temporary variables in my environment.

I would do this even if my login shell was bash, of course.

In fact, I even have a little shell function I use for initiating a clean bash session. It essentially runs env -i bash (which starts bash without transferring any environment variables to the new session) but also creates a temporary work directory. Upon exiting, the directory is removed by the shell function.

1 An environment variable is a shell variable that has been exported.


If anyone is interested:

function shell
{
    # Starts the specified shell in a clean environment (save for HOME,
    # TERM and SHELL) and with an empty temporary working directory.
    # The working directory is deleted upon exiting the shell session.
    # If no shell name is given, the shell is infered from $SHELL.

    typeset shell="$( basename "${1:-$SHELL}" )"

    typeset realshell="$( grep "^[^#].*/$shell\$" /etc/shells )"

    if [[ -z "$realshell" ]] || [[ ! -x "$realshell" ]]; then
        printf 'No such shell: %s\n' "$shell" >&2
        return 1
    fi

    tmpcwd="$( mktemp -d "${TMPDIR:-/tmp}/shell-$shell.XXXXXXXX" )"
    trap 'printf "Removing %s\n" "$tmpcwd" >&2; rm -rf "$tmpcwd"' EXIT

    printf 'Starting %s in %s\n' "$realshell" "$tmpcwd" >&2

    (
        cd "$tmpcwd" &&
        env -i  SHELL="$realshell" \
                TERM="$TERM" \
                HOME="$HOME" \
                "$realshell"
    )
}

Using it (extra newlines inserted for readability):

$ shell bash
Starting /usr/local/bin/bash in /tmp/shell-bash.mqUhTkBF

bash-4.4$ ls

bash-4.4$ env
PWD=/tmp/shell-bash.mqUhTkBF
HOME=/home/kk
TERM=rxvt
SHELL=/usr/local/bin/bash
SHLVL=1
_=/usr/bin/env

bash-4.4$ exit
exit
Removing /tmp/shell-bash.mqUhTkBF

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