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?
-
11I think these topics could be interesting for you: How do I know how many sub-shells deep I am? and How can I get help on terminal commands?– pa4080Commented Aug 1, 2020 at 9:44
6 Answers
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.
-
3If 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. Commented Aug 1, 2020 at 18:46 -
1@sudodus no need for password indeed but you still need to type
sudo
…– gboffiCommented Aug 1, 2020 at 19:40 -
2I 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. Commented Aug 1, 2020 at 21:13 -
1Creating a new subshell would re-source
.Xrc
, whereX
can be replaced by shells likebash
,zsh
,ash
, etc.– KulfyCommented Aug 2, 2020 at 8:33 -
2@Paul_Pedant You're right, but the more appropriate way to do it is
sudo -s
(shell) orsudo -i
(login shell).– alloCommented Aug 2, 2020 at 10:03
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
-
22
cat xxx | grep yyy
is a useless use ofcat
:grep
can read files by itself:grep user_name /etc/passwd
.– RuslanCommented Aug 1, 2020 at 18:39 -
10
-
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. Commented Aug 2, 2020 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.– ilkkachuCommented Aug 2, 2020 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. Commented Aug 3, 2020 at 14:09
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)
-
-
1@quantum231 a common reason to start a new shell is to be able to temporarily modify your environment for a particular task - e.g. set your $PATH, change your directory, set/change specific environment variables to launch a particular application. Then when you exit that shell you go back to the environment you were using in the directory you were in before you started it.– JohnGHCommented Mar 27 at 12:19
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.
-
1
zsh
isn't installed by default in Ubuntu. You can observe similar behaviour withsh
(which is a symlink to dash and is pre-installed). When you callsh
frombash
, the prompt would change to$
fromusername@host:~$
. You can try bash specific things to verify that you're insh
. For example, sh doesn't support brace expansions. Whenecho {1..5}
is run in sh, you'll get{1..5}
but in bash, you'll get1 2 3 4 5
.– KulfyCommented Aug 2, 2020 at 8:37
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...
The bash
command is an shell that is installed always and you CANT remove this shell you better see information here
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review– Pilot6Commented Feb 23, 2023 at 9:35