Linux Bash Exec Command Tutorial with Examples To Execute Command – POFTUT

Linux Bash Exec Command Tutorial with Examples To Execute Command


Linux bash provides different commands for daily usage. execcommand is mainly used to run commands in a bash environment. We can also provide parameters to this command.

Syntax

Syntax of exec command is like below.

exec OPTIONS COMMAND ARGUMENTS

Run Program or Script Directly

Without using exec we can run programs and scripts too. But we should make these executable. We will use chmod u+x command which makes given program or script executable for the owner.

$ chown u+x myscript.sh

and than we can run it directly without needing exec command like below.

$ ./myscript.sh

Run Command

We can simply run a command by providing the COMMAND. Provided command will run without creating a new process. In this example we will run ls command. After command execution is finished the shell will be closed. We can see in the screenshot that Connection to poftut1 closed

$ exec ls
Run Command 
Run Command

Provide Parameter

We can also provide parameter to the given command. We will add parameter after the command. In this example we will execute ls / .

$ exec ls /

Run Command with Empty Environment

Linux bash provides environments to the forked processes. Environment variables are used provides information about user, system, binary files etc. By default exec command also inherits current bash environment. We can disable environment inheritance and provide empty environment with -c option.

$ exec -c whoami

LEARN MORE  Linux Bash Tutorial Index

Leave a Comment