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.

1

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.

1

$chownu+xmyscript.sh

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

1

$./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

1

$exec ls

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 / .