Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash exec uso: Execute commands in the current shell
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash exec uso: Execute commands in the current shell

Overview

The exec command in Bash is used to execute a command in place of the current shell process. This means that when you run a command with exec, the current shell is replaced by the command you specified, and it does not return to the original shell after the command completes.

Usage

The basic syntax of the exec command is as follows:

exec [options] [command [arguments]]

Common Options

  • -a : Allows you to specify an alternative name for the command being executed.
  • -l : Makes the command a login shell.
  • -p : Preserves the environment of the current shell.

Common Examples

  1. Replace the current shell with a new command:
    exec bash
    

    This command replaces the current shell with a new instance of Bash.

  2. Run a script and replace the current shell:
    exec ./myscript.sh
    

    This executes myscript.sh and replaces the current shell with the script.

  3. Using exec with a different command name:
    exec -a myalias /bin/ls -l
    

    This runs the ls command with an alias of myalias, showing the long format of the directory listing.

  4. Start a login shell:
    exec -l bash
    

    This command starts a new Bash shell as a login shell, replacing the current shell.

Tips

  • Use exec when you want to run a command that should take over the current shell session, especially in scripts.
  • Be cautious with exec, as it will not return to the original shell after execution; any commands following exec in the script will not be executed.
  • If you want to retain the original shell, consider using subshells or simply running commands without exec.