| by Arround The Web | No comments

Bash Shell: ‘Exec’, ‘Eval’, ‘Source’ – What is the Difference

Among the many features of the Bash scripting are the commands exec, eval, and source, which can be used to execute commands, run scripts, and set environment variables. In this article, we will discuss each of these commands and how they can be used in the bash scripting.

exec in Bash Scripting

This command replaces the current running process with a new process as it can be used to run a new command in the same shell, or to replace the current shell with a new one. For example, to replace the current shell with a new bash shell, you can use the command exec bash. This will create a new shell with the same environment as the current shell, but any changes made in the new shell will not be visible in the old shell.

exec <command>

 

Here is the bash script example code that uses this exec command:

#!/bin/bash
echo "Starting script..."
ls -l
echo "Script completed."

 

The exec command is used to replace the current shell process with a specified command or script so in this example, the script prints a message using the echo command, then executes the ls -l command using exec. Since exec replaces the current process with ls -l, the following echo command is never executed.

eval in Bash Scripting

The eval command is used to evaluate a command that is stored in a string variable as this is useful when you need to dynamically generate a command based on user input or other variables. For example, if you have a variable cmd that contains a command, you can use the command eval $cmd to execute the command stored in the cmd variable.

eval <argument>

 

Here is the bash example code that uses this exec command:

#!/bin/bash
# Example of eval command
cmd="echo Hello, world!"
eval $cmd

 

The eval command is used to execute a shell command that is constructed dynamically at runtime so in this example, the variable cmd is set to the string “echo Hello, world!”. The eval command is then used to execute the contents of cmd, which prints the message “Hello, world!” to the console.

source in Bash Scripting

The source command is used to execute commands that are stored in a file and is useful when you have a script that sets environment variables or defines functions that you want to use in your current shell. For example, if you have a script my_script.sh that sets environment variables, you can use the command source my_script.sh to execute the script and set the environment variables in your current shell.

source < file-name>

 

Here is the example bash code executes a shell script within a script using the source command:

#!/bin/bash
# Example of source command
source ./bashfile2.sh

 

The source command is used to execute the commands in a specified script within the current shell environment so in this example, the script my_script.sh is executed using source. The commands in bashfile2.sh are executed in the current shell environment, which can be useful for setting environment variables or defining functions that will be used.

Conclusion

The bash provides powerful commands such as exec, eval, and source that can be used to manage Linux systems. The exec command is used to replace the current process with a new one, the eval command is used to evaluate a command stored in a variable, and the source command is used to execute commands stored in a file. These commands can be very useful in scripting and automation tasks and can help you to work more efficiently in the bash shell.

Share Button

Source: linuxhint.com

Leave a Reply