Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does the Unix `bash` command do when run without options?

When you type bash into the terminal and press enter, you go into what looks like an interactive bash interpreter... Which is as far as I know, what Terminal pretty much is, anyway.

The only visible difference is that the command prompt line says

bash-3.2$

instead of

Marcos-MacBook-Pro-3:Desktop marcoprins$

So what is happening when you run bash without options?

like image 987
Marco Prins Avatar asked Mar 18 '15 14:03

Marco Prins


2 Answers

The short answer is that when you type "bash" at a bash prompt, it starts a new bash process.

Bash is a program that reads command and executes them. It can read them from a file, or you can type them from an interactive prompt.

When you run a terminal, it's simply a window that runs bash in interactive mode, possibly reading some initialization code first. When you type "bash" at one of these prompts it simply starts another instance of the bash program (ie: another process), running "inside" the original bash program (process) running in the window. When you exit this new bash program, you will be returned to the original bash program where you can type more commands.

The prompt may or may not be different based on a whole bunch of reasons, many of which can be fine-tuned with bash command line options. Even if the prompt looks the same, you are operating in a different process from the original bash.

like image 175
Bryan Oakley Avatar answered Sep 30 '22 10:09

Bryan Oakley


The INVOCATION section in the man pages is pretty clear.

Posting a section of it here:

   A login shell is one whose first character of argument zero is a -, or one started with the --login option.

  An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals  (as  determined
   by isatty(3)), or one started with the -i option.  PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

  The  following paragraphs describe how bash executes its startup files.  If any of the files exist but cannot be read, bash reports an error.  Tildes are expanded in file
   names as described below under Tilde Expansion in the EXPANSION section.

  When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from  the  file  /etc/pro-
   file,  if that file exists.  After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the
   first one that exists and is readable.  The --noprofile option may be used when the shell is started to inhibit this behavior.

  When a login shell exits, bash reads and executes commands from the files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.

  When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.  This may be inhibited by using  the
   --norc option.  The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

  When  bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there,
   and uses the expanded value as the name of a file to read and execute.  Bash behaves as if the following command were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used to search for the file name.
like image 31
baky Avatar answered Sep 30 '22 12:09

baky