How to Reload or Change your current Shell?

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

When updating a .bashrc or alike (.bash_profile, .bash_aliases, etc.) on your workstation or even on a production 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 exec builtin command to help address such scenario.

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.

[root@host ~]$ 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.

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 cna be convenient in a cronjob or various scripts for logging purpose.

Related linux posts that you may like
What are the Differences Between Vi and Vim?
Review of the main differences between the Vi/Ex POSIX standard and the Vim implementation.
sudo: no tty present and no askpass program specified
Learn what is askpass and how to solve the 'sudo: no tty present and no askpass program specified' error when using sudo to execute a command.
What is the Bash Null Command?
Learn about the Bash null command, also known as the POSIX shell colon command. This post cover concrete use cases and pitfalls to avoid.