When updating a
.bashrc in bash or alike (
.bash_aliases, etc.) on your Linux workstation or server, the last thing you want to do is to logout from your terminal and login again to see those changes in action. The
POSIX specification define the shell
exec builtin command to help address such scenario and reload or restart your current shell.
What is the bash
exec is used to execute a command that will replace the current process image with a new process image. This mean that no new PID is created and any resources used by the former process would be freed.
execis specified with command, it shall replace the shell with command without creating a new process. If arguments are specified, they shall be arguments to command. Redirection affects the current shell execution environment.
[me@host ~]$ help exec exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...] Replace the shell with the given command. Execute COMMAND, replacing this shell with the specified program. ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified, any redirections take effect in the current shell. Options: -a name pass NAME as the zeroth argument to COMMAND -c execute COMMAND with an empty environment -l place a dash in the zeroth argument to COMMAND If the command cannot be executed, a non-interactive shell exits, unless the shell option `execfail' is set. Exit Status: Returns success unless COMMAND is not found or a redirection error occurs.
How to reload your bash shell?
If you add or change a
bash alias in your
~/.bash_aliases file, you can simply use
exec bash which will replace your current shell image with a new one, loading your configuration file and allowing you to use your updated aliases in your current terminal.
[me@linux ~]$ exec bash
📎 You can also use the
$SHELLenvironment variable with the command
exec $SHELLwhich would be compatible with all your shells, though be aware your active shell may not be the same.
This way is also useful if you want to change your current shell without spawning another process, for example, you login on a remote server, end-up with an old fashioned
sh bourne shell, you can simply run
exec bash to swap to a fresh new bash environment.
How to reload your Linux GNOME shell?
On a Linux desktop, you also have a Graphical Shell which is different from your command line terminal shell. If you are using a Linux distribution with GNOME, like on the latest Ubuntu 20.04 LTS, you may have two different way to reload your graphical shell depending on which version you are running.
GNOME 3.30.1 and above
Since version 3.30.1 of the GNOME shell, using the command line tool
gnome-shell will log you out and send you back to the login screen. Instead, prefer the below option which are identical.
[me@linux ~]$ killall -3 gnome-shell
[me@linux ~]$ killall -SIGQUIT gnome-shell
Before GNOME 3.30.1
To reload your graphical shell on GNOME before the 3.30.1 version need to use the
gnome-shell command line. You can use Alt+f2 to get a command prompt or run the command from a running command line terminal.
[me@linux ~]$ gnome-shell --replace &
Pitfalls with non-interactive shells
⚠️ As mentioned in the
help exec, if you plan to use exec with a non-interactive shell, you may want to check the
execfailis set, a non-interactive shell will not exit in the event the COMMAND fail to execute. An interactive shell does not exit if
Tips: use the
exec command to redirect all outputs
exec can be used to redirect all output to a file when run without any command, for example, run
exec > output.log, then any new command ran will have its output redirected to the log file, this will include all
stderr. This can be convenient in a cronjob or various scripts for logging purpose.