Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this command do? "exec bash -l"

What does this command do?

exec bash -l

I found this command as part of a reminder text file were I wrote some instructions regarding how to create a ssh key and clone a git repo, but I wrote it a long time ago and I can't remember what it does.

like image 256
matiascelasco Avatar asked Nov 05 '14 00:11

matiascelasco


People also ask

What does exec do in bash?

Linux exec Command Syntax If an argument is present, the exec command replaces the current shell with the executed program. In a Bash script, any commands after do not get executed. If no command argument is present, all redirections occur in the current shell.

What is exec $Shell L?

exec replaces the current shell with a new program. Thus, exec $SHELL replaces the shell with a completely new instance of the program specified in the variable named SHELL . And if you just run bash , not bash -l , it doesn't run your . bash_profile , because .

What is exec bin bash?

exec is used to spawn a process that will overtake current process's PID, i.e. simply replace your script process with whatever was inside that ${}

What will happen if we execute exec ls?

"exec ls" overrides your current shell with the "ls" process. Thus as soon as "ls" terminates your terminal closes.


2 Answers

exec executes a specified command, replacing the current process rather than starting a new subprocess.

If you type

bash -l

at a shell prompt, it will invoke a new shell process (the -l makes it a login shell). If you exit that shell process, you'll be back to your original shell process.

Typing

exec bash -l

means that the new shell process replaces your current shell process. It's probably slightly less resource intensive.

The reason for doing it is probably so that the new shell sets up its environment (by reading your .bashrc, .bash_profile, etc.).

See the bash documentation for more information:

  • Bash Startup Files for how a login shell differs from a non-login shell
  • Bourne Shell Builtins for documentation on the exec command.

(You should be able to read the manual on your own system by typing info bash.)

like image 111
Keith Thompson Avatar answered Sep 20 '22 05:09

Keith Thompson


This will replace your current shell with a new bash shell run as a login shell.

like image 25
eurythmia Avatar answered Sep 22 '22 05:09

eurythmia