I just typed
bash in Ubuntu's terminal and, it was like normal. But after that, I had to type exit twice. What is
bash command in bash?
bash is a command interpreter, a shell, a program with an interface that interprets the commands that you put into it.
When you call
bash, you simply start a new shell 'inside' the original shell. When you exit from this second shell, you return to the original shell. You can exit from each shell with the
There is a difference between shells and terminal emulators.
Shell is a thing that passes your commands to the kernel, and that is executed. And terminal emulator programs let you interact with the shell. Examples of Terminal emualtors are
konsole and shells are
sh etc. Terminal emulators are simply named as Terminal in most desktop environments.
When you open Terminal, it uses a shell by default. For most terminals it is
bash. You can change the default shell. First run
whoami to get your user name. Then run
cat /etc/passwd | grep user_name where
user_name is your user name. The last word is your default shell. Now you can change your shell with
sudo usermod --shell /bin/shell_name user_name.
Also when you type
bash it just opens another shell. You can simply exit the other shell by running
exit. Such as executing
sh will take you to other shell. You can read the man pages of shell with
man shell_name to learn the differences between shells. However the man pages are extremely large and complicated to read that it will make your head spin. Executing a
man shell_name | wc -l will give you the line count.
Hope that helps
When you run bash in an existing shell, this starts a new bash shell as a child process of the one you were using.
You can see this in your Linux environment with the ps command:
$ ps xjf PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 20282 20286 20282 20282 ? -1 S 26075 0:00 sshd: john@pts/0 20286 20287 20287 20287 pts/0 32674 Ss 26075 0:00 \_ -bash 20287 32135 32135 20287 pts/0 32674 S 26075 0:00 \_ bash 32135 32674 32674 20287 pts/0 32674 R+ 26075 0:00 \_ ps xjf
The reason you have to type exit twice to get out is that the first exit is exiting the child bash (process ID 32135 in this example), then the second exits the original bash (PID 20287 here).
If for some reason you wanted to start a new bash (or other shell) and knew that you were not going to want to return control to the original (parent) bash shell but instead to end your session, you could start the new bash via the exec command, which replaces your current running shell with the new process. (it actually keeps the same process ID, even if you change to a new shell like ksh via the exec command):
$ ps -f UID PID PPID C STIME TTY TIME CMD john 3463 20287 0 13:47 pts/0 00:00:00 ps -f john 20287 20286 0 06:11 pts/0 00:00:00 bash $ exec ksh $ ps -f UID PID PPID C STIME TTY TIME CMD john 3471 20287 0 13:47 pts/0 00:00:00 ps -f john 20287 20286 0 06:11 pts/0 00:00:00 ksh
(note both the original bash and the ksh that replaced it have PID 20287)
By using exec this way, when you exit the child bash, the parent has already gone, so you will end your session.
Side notes: One benefit of the exec bash is a quicker logout.
exec also uses less resources because the original shell has been replaced (the opposite of a bash fork bomb which consumes resources by starting many shells)
Beyond just launching shells, exec can let you give someone else access to something via your login, and make sure they're not left in your shell after they exit - e.g. I go to a colleague's desk, login as me and exec sudo (some command) or exec ssh (somewhere else) for them. Once they end that, it closes their terminal session instead of dropping them back to my login shell. (...but don't mess up logging in, or it will close your session when it would have normally returned to your shell)
When in the terminal you type, e.g.,
zsh ret you enter the Zshell and you operate using the syntax and the builtins of that shell, until you type
You would probably notice the difference if your default shell were Bash!
Now what happens when, from the command prompt of your default shell (i.e., Bash), you type
bash ret? Exactly what happened before with the Zshell, except that it's harder to tell what is different because you'll use the same syntax, the same builtins and the same aliases/shell functions as before!
But there are a number of differences, possibly the most apparent the environment variable
SHLV that Bash increments each time it's started.
Late to the party, but IMO, other answers don't get right to the point.
bash is a program whose purpose is to interpret your commands by running other programs. It's quite sophisticated--there's a lot of fancy ways you can construct a
bash command--but the simplest command that you can type to
bash is just the name of some other program.
If you type
foo at the command prompt,
bash will search for a program named
foo, and if it finds one, it runs the program, it waits for the program to finish, and then it prints another prompt.
So, guess what happens if you type
bash at the command prompt...