The exec command in Linux [With Easy Examples]

Exec Command In Linux

The exec command in Linux is used to execute a command by replacing the current process with that command. In bash shell and ksh shell, it is also used to redirect file descriptors for a complete session or for a whole script. In this tutorial, we will see and learn the various uses of the exec command.

exec command Basic usage

exec command on Linux can be a considered an optimization over the bash -c <program> alternative. Using the latter actually spawns a child process on Linux. Bash waits for the child to finish and then exits itself.

Using exec, the program replaces the calling process (the parent process) and no child process is spawned. This behavior might be useful if the parent is not required. Let’s see how you can execute a command using exec

1. Execute a command

Executing another program/command is the main usage of exec. It is done by giving the name of the program as an argument to exec.

exec <program-name>

2. Open and close file descriptors

This command is also used to open or close a file descriptor. This is mostly useful in scripts. To open and close this, exec command should not be given any argument as follows:

exec 3< thisfile

This will open the file named thisfile for reading on file descriptor 3.

exec 4> anotherfile

This will open the file named anotherfile for writing on file descriptor 4.

To close the file descriptors, the exec command can be used again by typing the following command:

exec 3<&-
#or
# exec 4>&-

exec command Examples

exec is not one of those commands you use every day, but it is certainly a command you can see used a lot in wrapper scripts for programs. Let’s take a look at some of the practical examples of this command:

1. Set Environment variables for a program and then exec it

A program may require quite a few environment variables. It’s way easier and more maintainable running that program from a shell script that sets everything up and eventually execs the program.

An example of that may look like

#!/bin/sh
ENVIRONMENT=$(some complex task)
exec command

A real-world example is the Google Chrome browser. If you follow symbolic links for the chrome executable, you’ll find that Google Chrome is actually executed as a bash script, which eventually calls exec at the end of the script.

You can run the following one-liner which uses the cat and the tail commands and see exec being used in the chrome script.

cat /opt/google/chrome/google-chrome | tail -10

2. Redirect file descriptors in a script

Suppose you wanted all the output such as the standard output of a script to go to a specific file or all the input coming from a specific file, you could do it through redirection on the command line like this:

script.sh > output.txt 2> /dev/null

But if you want to hide the details from the user, you can do that redirection inside the script. Let’s take a simple script that compiles and runs a C++ program. Just focus on the exec part of the script.

#!/bin/bash
PROJECT_DIR="."

exec 2> /dev/null               # Discard all errors
exec 1> $PROJECT_DIR/output.txt	# redirect all output of this script to a output.txt file 

if [ ! -e BoolienInvasion ]				
then 
	bison -d $PROJECT_DIR/BoolienInvasion.y 
	flex $PROJECT_DIR/BoolienInvasion.l 
	g++ -w -o $PROJECT_DIR/BoolienInvasion $PROJECT_DIR/lex.yy.c $PROJECT_DIR/BoolienInvasion.tab.c
fi

cat $@ > finalinput.input 				
$PROJECT_DIR/BoolienInvasion < finalinput.input

rm finalinput.input

Similarly, you can redirect the output of the command to a log file as well.


3. Change your login shell

Some sites only allow the use of certain login shells specified in /etc/passwd. You can still use your favorite shell through the exec command. Just enter

exec <shell> # example - exec ksh

in the .profile of that old shell. After this exec command, your favorite shell will replace the old shell as if you had logged in your favorite shell.


4. exec with the find command

The Linux exec command can also be used along with the find command to directly execute a command/script if it is found in the specified directory. For example:

sudo find ~ -name "filename.sh" -exec chmod +x '{}' \;

Conclusion

The use of exec command in shell script is not really ideal. But it comes quite handy on occasions. You can run help exec to know more about the options for exec. Do note that exec does not create a new process by itself. Also, this command replaces the current shell process with the executed command, so in a script, any subsequent command does not get executed. You can also visit its man page.

I hope this article demystified exec. Which uses of exec you didn’t know about? Let us know in the comments.