13

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?

4
  • 6
    You're executing an instance of bash in a subshell. Typing exit will bring you back to the main shell.
    – eigenchris
    Mar 18, 2015 at 14:51
  • 2
    With all applications closed except Terminal, and exactly one Terminal window open, type ps xc into the window. This will print out a great deal of Stuff. Now open another Terminal window. Type ps xc into that window. Compare the two lists. What changed?
    – zwol
    Mar 18, 2015 at 14:52
  • @eigenchris Technically this isn't a subshell, that's what happens when you use parentheses Mar 18, 2015 at 14:53
  • @ComputerDruid Isn't it the same idea? You are launching a shell which is a child process of another shell. I thought that would constitute a "subshell".
    – eigenchris
    Mar 18, 2015 at 15:02

3 Answers 3

18

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.

1
  • how to turn on interactive mode with bash? do you know? I assume bash detects if it's running in a tty and if so turns on interactive mode automatically? May 30, 2019 at 19:19
0

Bash runs your .bashrc when it is interactive (started with bash -i), which is true of the bash spawned by terminal emulators.

1
  • 1
    NO not bash -i. Just bash Mar 19, 2015 at 11:45
0

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.

1
  • 11
    Downvote for telling the OP to RTFM (and for an answer consisting only of a manpage quote that doesn't actually address the question). The bash manpage only makes sense if you already know what it's trying to tell you. It's great for "exactly what were the arguments to that shell builtin?" but it's no help at all if you're unclear on basic concepts.
    – zwol
    Mar 19, 2015 at 17:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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