The “exec” Command in Linux [8 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The exec command in Linux is an easy-to-use command. One can use it for multiple purposes. The exec command replaces the current terminal process with a new command. This command in Linux often executes specific programs or commands without creating a new process. In this article, I will demonstrate to you the ins and outs of the exec command in Linux.

A. Description

The exec command can be used for multiple purposes in Linux. The main purpose of the command is to replace the current process with a new command that takes over the memory, process ID, and some other resources. If the exec command is executed without any other command, it terminates the terminal. So, the bash command needs to be executed before that.

B. Syntax

The exec command has a very simple syntax in Linux. It includes the exec command, options & filename. And the syntax is as follows:

exec [OPTION]... [ARGUMENT]...

Note: In the syntax above, OPTION is enclosed by a square bracket and followed by 3 dots representing that more than one option can be used at the same time. Besides,3 dots after ARGUMENT suggest that you can specify multiple arguments.

C. Options

A few options are available for the exec command. I have mentioned one of the options of the command here. However, you can explore the help page for the exec command to know more about its options.

help exec

Useful Options

  • -c, Executes command with an empty environment

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be cautious when you use them.

Practical Examples of the “exec” Command in Linux

The exec command in Linux is a pretty straightforward command which replaces the current terminal process with a new command. Here you will learn some useful examples of the exec command in Linux.

Example 1: List the Contents of the Current Directory in Long Format Using the “exec” Command in Linux

Using the exec command you can list the contents of the current directory in Linux. To list the contents of my current directory, I am using the exec command in Linux. To do the same you can follow the procedure below:

Steps to Follow >

➊ Firstly launch the Ubuntu Terminal.

➋ Run the command below in the command prompt:

bash

➌ Now, press the ENTER button.

❹Copy the following command in the command prompt:

exec ls -la

➎ Then, tap the ENTER button.

Output >

As you can see, the contents of the current directory are listed in the output.List the Contents of the Current Directory in Long Format Using the “exec” Command in Linux

Note: Before running the exec command you need to run the bash command otherwise, the terminal will exit.


Similar Readings


Example 2: List All Environment Variables And Their Values

Using the exec command you can also list the environment variables and their values in Linux. I am using the exec command in Linux to list the environment variables and their values. To do the same you can follow the process below:

Steps to Follow >

➊ At first open the Terminal in Ubuntu.

➋ Write the following command in the command prompt:

bash

➌ Now, hit the ENTER key.

❹ Type the command below in the command prompt:

exec env

➎ Then, press the ENTER button.

Output >

In the image below, the environment variables and their values are listed in the output.List All Environment Variables And Their Values

Note: You need to execute the bash command before executing the exec command or else, the terminal will be terminated.

Example 3: Call Program in Bash Script Using the “exec” Command in Linux

In the bash script, a program can be called using the exec command in Linux. I am using the exec command inside a program in the script named script.sh in Linux. To do this you can follow the steps below:

Steps to Follow >

➊ Initially open the Ubuntu Terminal.

➋ Write the following command in the command prompt:

nano script.sh

Call Program in Bash Script Using the “exec” Command in Linux➌ Now, hit the ENTER key.

❹ Type the command below in the text editor:

#!/bin/bash

while true

do

echo "1) Update "

echo "2) Upgrade "

echo "3) Exit"

read Input

case "$Input" in

1) exec sudo apt update ;;

2) exec sudo apt upgrade  ;;

3) break

esac

done

Call Program in Bash Script Using the “exec” Command in Linux➎ Then save the script and exit by pressing the CTRL + S and CTRL + X buttons.

❻ Copy the command below in the command prompt:

. script.sh

❼ After that, tap the ENTER button.

Output >

In the following image, the output shows that the program of the script named script.sh is called. You can type 1, 2, or 3 and press the ENTER key to run the update, upgrade, or exit function.Call Program in Bash Script Using the “exec” Command in Linux

Example 4: Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

You can log the standard output and the standard error in the bash script using the exec command in Linux. To log standard output & standard error “>>” and “>&” are used consecutively. I am using the exec command to log the standard output and the standard error in the script named logging.sh in Linux. You can also do this by following the instructions below:

Steps to Follow >

➊ To start, launch the Ubuntu Terminal.

➋ Run the following command in the command prompt:

nano logging.sh

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux➌ Then, hit the ENTER key.

❹ Type the following command in the text editor:

#!/bin/bash

# Create testFile.log file

touch testFile.log

# Save testFile.log to log_file variable

log_file="testFile.log"

# Redirect stdin to $log_file

exec >>$log_file

# Redirect stderr to the same place as stdin

exec 2>&1

echo "This line is the first line of the log file"

echo "This line is the second line of the log file"

eho "This line is the third line and has an error"

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux➎ Then save the script and exit by pressing the CTRL + S and CTRL + X buttons.

❻ To give execution permission to the logging.sh script, copy the following command in the command prompt:

chmod +x logging.sh

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux❼ Now, strike the ENTER button.

❽ After that, write the following command in the command prompt:

./logging.sh

Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux❾ Then, tap the ENTER key.

❿ Run the command below and press the ENTER button.

cat testFile.log

Output >

In the image beneath, the output shows that the textFile.log file contains the standard output and the standard error.Log Stdout & Stderr In the Bash Script Using the “exec” Command in Linux

Example 5: Create a Clean Environment By Running Script

You can create a clean environment using the exec command with the printenv command and option -c in Linux. I am using the exec command in Linux to clean the environment variables and their values. You can also do the same by following the steps below:

Steps to Follow >

➊ Initially open the Terminal in Ubuntu.

➋ Write the following command in the command prompt and hit the ENTER key.

printenv

Create a Clean Environment By Running ScriptThis command prints all environment variables and their values in the terminal.

➌ Now, Type the command below in the command prompt:

bash

❹ Then, press the ENTER button.

➎ After that, copy the following command in the command prompt:

exec -c printenv

❻ At last, tap the ENTER key.

Output >

In the following image, as you can see in the output the environment variables and their values are cleaned by using the exec command with the printenv command and option -c.

Note: You need to execute the bash command before executing the exec command if not, the terminal will exit.

Example 6: Search Files And Display Their Contents Using the “exec” Command in Linux

You can search the desired files using the find command & display the contents of the files using the cat command in Linux. And the exec command uses the output of the find command as the input of the cat command. Here I want to search all the .txt files of the MyFolder/ directory which is inside the home directory using the find command and use the output as the cat command’s input using the exec command. To do the same you can follow the procedure below:

Steps to Follow >

➊ At first, launch the Terminal in Ubuntu.

➋ Copy the following command in the command prompt:

find ~/MyFolder -name "*.txt" -exec cat {} \;

➌ Then, strike the ENTER key.

Output >

As you can see in the image, the terminal is displaying the contents of all the .txt files searched by the find command.Search Files And Display Their Contents Using the “exec” Command in Linux

Example 7: Search Files And Compress Those Using the “exec” Command in Linux

The expected files can be searched using the find command & compress each file that is found using the gzip command in Linux. And the exec command uses the output of the find command as the input of the gzip command. Here I want to search all the .log files of the MyFolder/ directory which is inside the home directory using the find command and using the exec command to forward the output as the gzip command’s input. To do the same you can follow the method below:

Steps to Follow >

➊ At first, launch the Terminal in Ubuntu.

➋ Type the following command in the command prompt and press the ENTER button.

ls MyFolder/

➌ Copy the following command in the command prompt:

find ~/MyFolder -name "*.log" -exec gzip {} \;

❹ Then, strike the ENTER key.

❺ Write the following command in the command prompt:

ls MyFolder/

❻ After that, hit the ENTER key.

Output >

As you can see in the image, all the .log files searched by the find command are compressed using the gzip command.Search Files And Compress Those Using the “exec” Command in Linux

Example 8: Search Files And Change Permission

You can give permission to each file that is found using the chmod command in Linux. And the exec command forwards the output of the find command to the input of the chmod command. Here I want to change the permission of all the .log files of the MyFolder/ directory which is inside the home directory. You can also do the same by following the process below:

Steps to Follow >

➊ At first, open the Terminal in Ubuntu.

➋ Write the following command in the command prompt and press the ENTER button.

ls -la MyFolder/

➌ Run the following command in the command prompt:

find ~/MyFolder -name "*.log" -exec chmod +x {} \;

❹ Then, strike the ENTER key.

❺ Copy the following command in the command prompt:

ls -la MyFolder/

❻ After that, hit the ENTER key.

Output >

As you can see in the image, the permission of all the .log files searched by the find command is changed by using the chmod command.Search Files And Change Permission

Conclusion

As seen in this article, the exec command has some significant uses in Linux. You’ve also found the syntax, some functional options, and the practical applications of this command. To become an expert in Linux, practice the command and its practical applications.


Similar Readings

 

Rate this post
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment