Fundamentals 4 min read

Mastering the exec Command: How to Replace Processes in Linux Shell

This article explains the Linux exec command, its syntax and options, provides practical examples, and expands on how exec replaces a process without creating a new PID, offering deeper insight into process management and system calls.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering the exec Command: How to Replace Processes in Linux Shell

exec: Call and Execute Specified Command

Function Description

The exec command is used to call and execute a specified command. It is commonly used in shell scripts to invoke other commands. When used in the current terminal, the terminal exits immediately after the command finishes.

Command Syntax

exec [options]

Option Meanings

-c : Execute using an empty environment

-a : The shell passes the name as the zero‑th argument to the executed command

-l : Place a dash at the beginning of the command line, passing it as an argument

Reference Examples

Example 1

// First use echo to output "www.linuxyz.cn"
# echo www.linuxyz.cn
www.linuxyz.cn

// Then use exec to call echo with the -c option
# exec -c echo www.linuxyz.cn
www.linuxyz.cn

// The output is identical, showing that exec successfully invoked echo.

Example 2

# find /test -name "te.txt" -exec cp {} {}.bak \;

Knowledge Expansion

1. The exec system call replaces the current process with a new one, but the PID remains unchanged. Therefore, exec does not create a new process; it swaps the process context, replacing the code, data, and stack segments.

A process mainly consists of:

An executable program

All associated data (variables, memory, buffers)

Program context (program counter PC, etc.)

2. exec is a family of functions comprising six variants, such as execl, execv, and others.

Typical usage involves calling fork() to create a new process, then using exec in the child process. After fork(), the parent and child share the code segment but have separate data spaces. Modern systems employ a copy‑on‑write strategy: the address space is shared until a write occurs, at which point the necessary pages are copied, making the child’s data independent. When exec is invoked after fork, the child’s previous data is replaced by the new program, improving efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSystem Callprocessexec
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.