4

This question already has an answer here:

When I open Terminal Window in Mac OS X, I can type bash and hit enter and I will see:

Last login: Fri Feb 20 14:30:56 on ttys000
Korays-MacBook-Pro:~ koraytugay$ bash
bash-3.2$ 

I can for example run commands such as ls or ls -l both in Terminal and in "bash-3.2$"

What is bash running under Terminal and what is Terminal itself?

marked as duplicate by Raystafarian, fixer1234, Scott, DavidPostill, Nifle Feb 25 '15 at 22:18

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.

4
+50

Bash is one of the popular command-line shells, programs whose chief job is to start other programs (in addition to some auxiliary functions).
The command-line part means you control it by typing commands one line at a time.
Properly speaking, a GUI you use to start programs by double-clicking on icons is also a shell, but in practice by "shell" people mostly mean command-line ones.

All modern command-line shells take their input and send their output as abstract streams of characters, and the other ends of those streams can be connected to a keyboard, a printer, a file, another program. The shell mostly doesn't care - it reads the characters, interprets them as commands telling it to run other programs, and writes back characters such as "command not found". When it runs another program, by default it connects the inputs and outputs of that program to the same streams.

Now, Terminal is a program that provides a graphical interface between the shell and the user. It receives from the shell e.g. the characters "command not found" and figures out how to display them to you - with what font, where on the screen, in what colour, whether there should be a scrollbar. When you press some keys, it figures out whether to send them on to the shell as characters (e.g. ls -l), or to interpret them on its own (e.g. ⌘C).

When you open the Terminal app, it automatically opens a shell to connect you to. In its settings, you could choose a different shell from Bash. If you're feeling cheeky, you could even make it use a program that isn't a shell at all - not too useful, but it demonstrates how Terminal cares only about passing characters in and out, not about what the shell does with them.

What happens when you type bash into Bash (through Terminal)? It starts the program Bash - that is, another copy of itself inside itself.

ETA: The prompt that Bash gives you before you're typing each command is usefully customizable, and controlled (using a special format) by the variable PS1. Try typing echo $PS1 in both the parent and child instance of Bash.

When run from the Terminal app, that variable is set to prompt you with the machine name, directory and user. This is set up for you in /etc/bashrc, but you can set a new value, ideally in ~/.bash_profile. NB. that's somewhat OSX-specific; on most other systems, you'd prefer ~/.bashrc for that.

When you just run a child instance of Bash, /etc/bashrc isn't re-executed, so this variable isn't set. It's also not set up to be inherited by child shells (which is totally a thing for environment variables) so Bash reverts to the unhelpful default of just showing you which version of it is running.

  • But why doesn't it say bash the first time it is opened? Btw this was the answer I was looking for thanks, it is clear now.. – Koray Tugay Feb 23 '15 at 18:57
  • I will give the bounty to you once the required time expires. Thanks, this is a great great answer.. However, in my ~/.bash_profile file I have something like: export PATH=$PATH:/Users/koraytugay/Applications/db-scripts which makes me use 'mydbscript.sh' from anywhere. You say bash_profile is not loaded again for the child bash, but in terminal when I launch an extra bash and type 'mydbscript.sh' it still works, so I am confused at this point.. – Koray Tugay Feb 24 '15 at 7:06
  • Cheers! The export keyword does exactly that: marks the variable for "export" to child processes. Try echo $PATH in both the child and parent instance. I may have been imprecise about files above: on OSX, when Bash starts directly inside a Terminal window (or iTerm2 or similar replacement app) it runs /etc/bashrc (shared by all users) and then ~/.bash_profile (user-specific); when started from inside a shell it runs ~/.bashrc. It's common to invoke ~/.bashrc at the end of ~/.bash_profile – hemflit Feb 24 '15 at 11:18
  • Could you please see this: pastebin.com/pwcG7wgR I am confused because you said "When you just run a child instance of Bash, /etc/bashrc isn't re-executed, so this variable isn't set. It's also not set up to be inherited by child shells" but the paths are same for both the first bash and the bash inside the bash? – Koray Tugay Feb 24 '15 at 11:59
  • 1
    Check my previous comment: your profile script says "export PATH…", and exported variables are inherited by child processes - unlike your PS1 which isn't set to be exported. Also, to check the value of an environment var, use echo $VARNAME. – hemflit Feb 24 '15 at 13:19
8

A terminal once literally meant a box you typed into, remotely connected to a mainframe.

In fact, your modern 'terminal' possibly emulates one of these

enter image description here

Modern 'terminals' are terminal emulators which behave roughly like a standard terminal would. So you're running xterm, rxvt or something else. The terminal provides a mechanism for entering commands.

You run a command processor, or shell, on top of that - bash, fish, csh or others. This is what actually turns the text that is typed into the terminal into instructions that the computer acts on. Most shells allow scripting, and you'd see a very different syntax between say bash or csh. They would run similarly on different terminals.

1

see also https://stackoverflow.com/questions/3327013/how-to-determine-the-current-shell-im-working-on.

You can try echo $0 or echo $SHELL to find out what shell is running in your Terminal emulator window in the first place.

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