How to Reload or Change your current Shell?

  • HOME
  • >
  • LINUX
  • >
  • How to Reload or Change your current Shell?
Last Updated: 
subscribe to my newsletter

When updating a .bashrc in bash or alike (.bash_profile, .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 command?

In short, 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.

If exec is 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 aliases in your ~/.bash_aliases, 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 exec $SHELL which 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

OR

[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 execfail option. If execfail is set, a non-interactive shell will not exit in the event the COMMAND fail to execute. An interactive shell does not exit if exec fails.

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 stdin and stderr. This can be convenient in a cronjob or various scripts for logging purpose.

Related linux posts that you may like...
How To Create Simple Menu with the Shell Select Loop?
The select loop is not a regular shell loop. It can be used in Bash to generate a simple menu from which a user can select numbered options.
What is the Right Way to Loop in Bash?
Looping over a list of numbers or words is a building block in shell scripts. Learn how to write Bash loops, including for loop, while loop, and until loop.
What is the Best Way to Count Files in a Directory?
Learn how to count the number of files in a directory using the Linux command line ls, find, and a native bash shell solution with globs and arrays.
5 Mistakes To Avoid For Writing High-Quality Bash Comments
Adding comments in your Bash scripts is necessary to ensure maintainability over time. This post covers 5 Bash comments mistakes to avoid in your shell scripts.