The exec command in Linux is used to execute a command by replacing the current process with that command. In shells such as bash
and ksh
, it is also used to redirect file descriptors for a complete session or for a whole script.
exec command Basic usage
exec command can be a considered an optimization over the bash -c <program
> alternative. Using latter actually spawns a child process. 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
command. It is done by giving the name of the program as an argument to exec
.
exec <program-name>
2. Open and close file descriptors
The exec command is also used to open or close a file descriptor. This is mostly useful in scripts. To open and close file descriptors, 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 as follows
exec 3<&-
#or
# exec 4>&-
exec command Examples
exec is not one of those commands you use everyday but it is certainly a command you can see used a lot in wrapper scripts for programs.
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 exec
s 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 of the 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
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.
Conclusion
exec is a not a command you would use in every script. But it comes quite handy on occasions. You can run help exec
to know more about the options for exec. 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.