26

I'm on a Mac but I think this is generally Unix-applicable.

I'm in the process of learning shell scripting and there's something I seem to be missing. When I'm in the ordinary terminal, I can use scripting syntax like for loops and such in conjunction with commands to do stuff.

But....bash opens an interpreter for shell scripting.

Which is where I get confused, because isn't the terminal already an interpreter for shell scripting, as demonstrated by the fact that the scripting works when given to stdin?

Bonus question: how is bash different from bash -i, which according to man "starts an interactive session".....isn't that what happens when you just enter bash on its own? Which, to my eye is no different than being in the normal terminal in the first place...

marked as duplicate by Gilles bash Jan 25 '15 at 22:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The terminal is a container for the shell (or any other program). It provides input (through connected input devices) and displays output on two channels: stdout and stderr. Generally on most *nix systems you should be able to run $SHELL --version to see what shell you are running in. What hasn't been mentioned is that shells like Bash have three major modes: login, interactive, script. Login and interactive are very similar from a user's perspective. Script is when the shell is used to interpret commands in batch. – 0xC0000022L Jan 25 '15 at 12:16
  • @Gilles this is not a duplicate!!!! I already knew about that question and your answer and I have read it and it did NOT tell me why my terminal can run bash commands on its own and what the difference is when I execute bash again! Your answer makes no mention of the fact that the OS X terminal is executing bash by default! – temporary_user_name Jan 25 '15 at 23:04
  • @Aerovistae Your terminal doesn't run bash commands on its own, it lets you communicate with the bash process that it runs. – Gilles Jan 25 '15 at 23:08
  • That's what I meant, yes. But that question is asking for a disambiguation of terminology, while this question is asking how the OS X terminal is related to the bash shell, which is now clear to me. Your question is related and helps but is not the same. – temporary_user_name Jan 25 '15 at 23:18
  • I agree with @Aerovistae. I stumbled over both the questions since I had the same questions. And if one does not what the terms "Terminal", "Shell" "Bash" even mean, it's difficult to formulate a precise question. And these answers here are really helpful! – Felix Crazzolara Oct 14 '17 at 10:37
29

When you launch a terminal it will always run some program inside it. That program will generally by default be your shell. On OS X, the default shell is Bash. In combination that means that when you launch Terminal you get a terminal emulator window with bash running inside it (by default).

You can change the default shell to something else if you like, although OS X only ships with bash and tcsh. You can choose to launch a custom command in a new terminal with the open command:

open -b com.apple.terminal somecommand

In that case, your shell isn't running in it, and when your custom command terminates that's the end of things.

If you run bash inside your terminal that is already running bash, you get exactly that: one shell running another. You can exit the inner shell with Ctrl-D or exit and you'll drop back to the shell you started in. That can sometimes be useful if you want to test out configuration changes or customise your environment temporarily — when you exit the inner shell, the changes you made go away with it. You can nest them arbitrarily deeply. If you're not doing that, there's no real point in launching another one, but a command like bash some-script.sh will run just that script and then exit, which is often useful.


The differences between interactive and non-interactive shells are a bit subtle and mostly deal with which configuration files are loaded, which error behaviours there are, and whether aliases and similar are enabled. The rough principle is that an interactive shell gives you the settings you'd want for sitting in front of it, while a non-interactive shell gives you what you'd want for a standalone script. All of the differences are documented explicitly in the Bash Reference Manual, and also in a dedicated question on this site.

For the most part, you don't need to care. There's not often a reason to launch another shell, and when you do you'll have a specific purpose in mind and know what to do with it.

  • The arbitrary-level nesting is actually kind of awesome. Had to go test that by entering bash repeatedly and then ctrl+Ding my way back out. Really neat. – temporary_user_name Jan 25 '15 at 9:27
  • 2
    @Aerovistae You can check if your Terminal run bash or not just execute ps $$ – Costas Jan 25 '15 at 11:08
  • 1
    I can add that with exit or ctrl-d you are quiting from inner shell, but if you are already on the most outer shell then you will quit not only that shell but close terminal emulator as well. Thus, even so for that reason it is sometimes helpful to know how many shell levels are nested. You can check that with echo $SHLVL. – jimmij Jan 25 '15 at 11:28
  • @Aerovistae: in Bash use echo $SHLVL to see how deeply nested you are. It's the "shell level" and gets incremented when running another shell in the existing one. – 0xC0000022L Jan 25 '15 at 12:17
  • Another difference between interactive and non-interactive shells is in signal handling. An interrupt does not cause an interactive shell to exit; it does cause a non-interactive shell to exit. – Jonathan Leffler Jan 25 '15 at 15:39
28

The machine in this picture is a (video) terminal, more specifically a VT100 by Digital Equipment Corporation.

VT100 terminal from Wikipedia

Decades ago when computers were big, instead of having personal computers for each user, they could have had a terminal, a dummy device with display and keyboard, that is connected to a main computer via a cable. A VT100 is not a computer, but just a keyboard and a display. There usually were several of these connected to a single computer.

Thus decades ago a Unix computer was accessed via a terminal, which was a physical device. As the personal computer came along, and graphical user interfaces became commonplace, there was (and still is) a way to access the Unix command line as if by such a terminal device - applications called terminal emulator. The Terminal program in OS X is a terminal emulator; most of the terminal emulators of the present day still emulate that very same VT100 device quite closely, i.e. most of the programs run in a window of the Terminal application look exactly the same as they would look if run on the big computer of 70s and the data displayed on such a terminal device. To the programs, each window in your terminal emulator behaves like one of these devices; most command line programs couldn't notice a difference (though you could find it difficult to connect a genuine VT100 to your MBP).


A video terminal was the successor of a device called a hard-copy terminal, a device that would have a keyboard and printer - all the output from programs would be printed on the paper instead. One can imagine that a video terminal was a big improvement over such devices. An example of a hard-copy terminal, a TeleType Model 33 ASR:

TeleType Model 33 ASR

This device was also called teletypewriter, teletyper, or tty for short; and the tty stuck from the early 70s, and the interface for a such a device, or terminal emulator is still called a tty, and in many programming languages outputting text for display in a terminal window still is called "printing"; originally it was not a metaphor but a fact.


The shell has always been there - from the dawn of Unix, it was the program that was run after you entered your login name and password on the terminal, to access the central computer. The first shell program was the Thompson shell (sh) from 1971, which in 1977 was superseded by Bourne shell, also called sh. Early on, it was designed so that that this was just another program that could be updated easily, and that users could run their own program instead of the default shell.

The GNU project then produced from scratch an improved shell called bash, short for Bourne-again shell, which Apple decided to ship with MacOS X too.


Back in 1970s, the distinction was clear: a terminal was that what seemed like 30 kg piece of solid cast iron frame wrapped in cream-colour plastic case with a glass display and keyboard in front of you, or even a device with just a keyboard and printer, whereas a shell was a program running on the main computer interpreting your commands.

  • 2
    Welcome to Unix, +1 for the insightful answer. – Aaron Hall Jan 25 '15 at 19:51
  • @Antti Haapala - As a clarification, is a console the same as a terminal - (askubuntu.com/a/331435/490964)? What is the distinction between a text console and a terminal emulator? What do you mean by "each window"? Do you mean to say terminals windows between 1 - 7? – Motivated Jan 6 at 6:11
  • @Motivated yes they're like terminals, yes, but a console in itself does not mean a terminal - they're opposite things. The word console originally meant the input/output devices that belong to the computer itself, whereas a terminal is another device in addition to the possible console that is connected to the same machine - possibly even over a modem or so. So your console is your display, mouse and keyboard. The each window is in reference to windows/tabs in gui programs such as Konsole, Xterm and so on... – Antti Haapala Jan 6 at 7:06
  • @AnttiHaapala - To clarify, do you mean to say that a console is the display, keyboard and mouse attached to a computer e.g. mainframe? A terminal is an extension to the console to enable access to the computer? When you say windows/tabs, do you mean a second, third, fourth, etc Konsole window or tab? – Motivated Jan 6 at 7:53
  • 1
    No, consoles is not a single device. Console is the means how you primarily use a machine, when you go physically go to it, period. If you physically go to your laptop to use it, what you use is the console. – Antti Haapala Jan 6 at 15:09
7

You are missing your terminal is already running bash (or another shell interpreter) in the first place.

A terminal and more precisely a terminal emulator in your case is just a device passing keystrokes to an underlying program and displaying whatever characters are sent to it. While it runs a shell by default, nothings forbids to start a terminal running a different text application, like a text editor or whatever.

If you are already in an interactive session, running bash and bash -i wouldn't indeed make a difference.

  • 1
    So what does the bash command DO then? What is the purpose of it? Why does it change the prompt from computername:pwd to bash-3.2$ ? – temporary_user_name Jan 25 '15 at 9:22
  • 2
    The bash command runs a new shell on top of the existing one. The fact it changes the prompt (or more precisely reset it to the default) is due to an initialization script not being read. – jlliagre Jan 25 '15 at 9:25
  • 1
    @Aerovistae: the difference lies in the initialization files Bash runs in the different modes (see my comment on your q). If you run another Bash inside an existing Bash session it isn't going to be a login shell anymore as it would most likely be at the top level. – 0xC0000022L Jan 25 '15 at 12:19

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