exec command in Linux with examples
Last Updated :
30 Aug, 2024
The 'exec' command in Linux is a powerful shell built-in used to replace the current shell process with a new command process. Unlike typical commands that create a new process, 'exec' transforms the existing process, directly impacting the efficiency and behavior of scripts.
What is the exec Command?
'exec' overrides the current shell session with a specified command without creating a new process. This unique property makes 'exec' particularly useful for modifying the shell environment or executing commands with performance in mind, as it avoids the overhead of spawning a new process.
Syntax:
exec [-cl] [-a name] [command [arguments]] [redirection ...]
Options:
- '-c': It is used to execute the command with empty environment.
- '-a name': Used to pass a name as the zeroth argument of the command.
- '-l': Used to pass dash as the zeroth argument of the command.
Note: 'exec' command does not create a new process. When we run the exec command from the terminal, the ongoing terminal process is replaced by the command that is provided as the argument for the exec command.
Key Behaviors of 'exec' Command
The exec command can be used in two modes:
1. Exec with a command as an argument
In the first mode, the exec tries to execute it as a command passing the remaining arguments, if any, to that command and managing the redirections, if any.
Example 1:
Example 2:
The 'exec' command searches the path mentioned in the '$PATH' variable to find a command to be executed. If the command is not found the exec command as well as the shell exits in an error.
2. Exec without a command
If no command is supplied, the redirections can be used to modify the current shell environment. This is useful as it allows us to change the file descriptors of the shell as per our desire. The process continues even after the exec command unlike the previous case but now the standard input, output, and error are modified according to the redirections.
Example:
Here the 'exec' command changes the standard out of the shell to the tmp file and so all the commands executed after the 'exec' command write their results in that file. This is one of the most common ways of using exec without any commands.
Conclusion
The 'exec' command is a versatile tool in the Linux shell scripting arsenal. It allows for efficient process management by replacing the current shell with a command instead of creating a new one. By utilizing the various options and behaviors of 'exec', users can significantly enhance the functionality and performance of their scripts.
Similar Reads
du Command in LINUX The du command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether youâre trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valu
4 min read
dump command in Linux with examples The dump command in Linux is used for backing up the filesystem to a storage device. Unlike commands that back up individual files, dump operates on entire filesystems, transferring data to tape, disk, or other storage media for safekeeping. One of its key advantages is the ability to perform increm
5 min read
dumpe2fs command in Linux with examples dumpe2fs command is used to print the super block and blocks group information for the filesystem present on device. Can be used with ext2/ext3/ext4 filesystem for information. The printed information may be old or inconsistent when it is used with a mounted filesystem. Don't forget to unmount your
2 min read
dumpkeys command in Linux with examples 'dumpkeys' command in Linux is used for the dump keyboard translation tables. It writes to the standard output and the current contents of the keyboard driver's translation tables in the format specified by Keymaps. Part of the kbd package, it displays the key bindings, function key strings, and com
4 min read
echo command in Linux with Examples The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file. Syntax of `echo` command in Linuxecho [option] [string]Here, [option
3 min read
ed command in Linux with examples Linux 'ed' command is used to start the ed text editor, a very minimal fronted line-based text editor. The lines are very simple in relation to other text files, hence easy to work on, for creating as well as editing, display, and manipulation of files. Since it was the first editor that was include
4 min read
egrep command in Linux with examples The 'egrep' command in Linux is a powerful pattern-searching utility that belongs to the family of grep functions. It functions similarly to 'grep -E' by treating patterns as extended regular expressions, allowing more complex pattern matching without the need to escape special characters. This comm
3 min read
eject command in Linux with examples eject allows ejecting a removable media (typically a CD-ROM, floppy disk, tape, or JAZ or ZIP disk) using the software. You can also control some multi-disc CD-ROM changers, the auto-eject feature (supported by some devices), and close the disc tray of some CD-ROM drives. Syntaxeject [...OPTIONS]The
4 min read
emacs command in Linux with examples Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like
5 min read
env command in Linux with Examples The 'env' command in Linux is a versatile tool used to manage environment variables. It can print all the current environment variables, set new ones, or run a command within a custom environment. Additionally, 'env' is commonly used in shell scripts to launch the correct interpreter, ensuring the s
3 min read