9

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?

19

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 gnome-terminal,konsole and shells are bash,zsh,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 zsh or 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

| improve this answer | |
  • 22
    cat xxx | grep yyy is a useless use of cat: grep can read files by itself: grep user_name /etc/passwd. – Ruslan Aug 1 at 18:39
  • 10
    Why usermod over chsh? – D. Ben Knoble Aug 1 at 19:42
  • 9
    "Shell is a thing that passes your commands to the kernel".. misleading nonsense. What does this have to do with the kernel? A shell allows you to execute commands in your path, provides some facilities to the OS API (like listing directories and the like), and sometimes provides a simple language for automating tasks. – Señor CMasMas Aug 2 at 19:04
  • 3
    @SeñorCMasMas, what the shell has to do with the kernel is that the shell parses the command line and makes the system call asking the kernel to start a process. The shell doesn't exactly handle any of the low-level stuff of launching a process itself. – ilkkachu Aug 2 at 20:01
  • 3
    @ilkkachu, The word "kernel" is just a distraction from the answer to the OP's question. /bin/bash is a program whose purpose is to interpret the commands that you type by running other programs. Yeah, there's "kernel" and "system call" in the details of how it works. But OP's question wasn't about how it works. The question is about what it does. – Solomon Slow Aug 3 at 14:09
36

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 from 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 exit command.

| improve this answer | |
  • 3
    If you have a number of sudo commands to run, you can sudo bash to start a root bash, and exit from it back into your user-level bash. This saves multiple password entries, and it changes the prompt to remind you not to do anything bad while you are root. – Paul_Pedant Aug 1 at 18:46
  • 1
    @sudodus no need for password indeed but you still need to type sudo – gboffi Aug 1 at 19:40
  • 2
    I had RHEL at a client site, and their sudoers policy was set to 5 minutes. Unfortunately my thinking time is 6 minutes. My current use for typing bash is so I can set PS1 to not have my details when I post commands to forums, and then go back to normal easily. – Paul_Pedant Aug 1 at 21:13
  • 1
    Creating a new subshell would re-source .Xrc, where X can be replaced by shells like bash, zsh, ash, etc. – Kulfy Aug 2 at 8:33
  • 2
    @Paul_Pedant You're right, but the more appropriate way to do it is sudo -s (shell) or sudo -i (login shell). – allo Aug 2 at 10:03
5

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)

| improve this answer | |
3

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 exit ret.

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.

| improve this answer | |
  • 1
    zsh isn't installed by default in Ubuntu. You can observe similar behaviour with sh (which is a symlink to dash and is pre-installed). When you call sh from bash, the prompt would change to $ from username@host:~$. You can try bash specific things to verify that you're in sh. For example, sh doesn't support brace expansions. When echo {1..5} is run in sh, you'll get {1..5} but in bash, you'll get 1 2 3 4 5. – Kulfy Aug 2 at 8:37
2

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...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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