FUNDAMENTALS A Complete Guide for Beginners
The eval command is a built-in feature in Linux shells like Bash. If you need to execute the arguments as a shell command, you can use the eval command in Linux. This is useful for substituting variables, commands, and so on. In this article, I’ll demonstrate the most common uses of the eval command with relevant examples.
Syntax of “eval” Command
The syntax of the eval command is:
eval [arg ...]
Note: Everything inside square brackets is optional in this syntax. Three dots after the arg indicate that multiple values can be passed as arguments. On a different note, the eval command doesn’t take any option.
4 Examples of Using “eval” Command in Linux
Let’s explore the use of the eval command with relevant examples:
1. Using “eval” Command for Variable Substitution
You can use the eval command for variable substitution. Here are some examples:
Case 1: Single Variable Substitution
To substitute a single variable, you need to define a variable first, then use the eval command to substitute it. For example, let’s try out the following commands to store the value John in a variable named user1 and later substitute it using eval:
user1=John
eval echo $user1
From the image above, you can see the variable user1 is substituted with its value John by using the eval command.
Case 2: Nested Variable Substitution
To substitute a nested variable, you can use the eval command. Here, a nested variable refers to assigning an already declared variable to another variable. For example, let’s define two variables, user1 and member1:
user1="John Smith"
member1=user1
Here, we’ve defined the user1 and later, assigned the user1 to the member1 variable. Now, call the member1 variable:
echo \$$member1
This returns $user1. However, if we use the eval command at the beginning:
eval echo \$$member1
This eval command returns John Smith.
In the above image, you can see that, while the echo \$$member1
command line returns $user1, the eval echo \$$member1
command line interprets the member1 variable as user1 and the user1 variable stands for John Smith. So finally, the eval command returns John Smith which is the value of user1.
From this example, you can better understand how the eval command works.
Note: While defining the variables, use double quotes around their value to avoid any unexpected error. For example, we’ve used double quotes while defining the variable user1=”John Smith”.
Case 3: Environment Variable Substitution
To get the value of an environment variable, use only a dollar sign before the environment variable:
var1='echo $USER'
eval $var1
This returns the name of the current user.
On the other hand, if you want to use the environmental variables as a string, then, you need to use a backslash and a dollar sign before the variable name:
var2='echo \$USER'
eval $var2
These command lines consider the environment variable as a string and return the value as USER.
The image above demonstrates scenarios where the value of the environment variable is returned and when the name of the environment variable is returned.
2. Using “eval” Command for Command Substitution
Command substitution in Linux allows you to use the output of one command as input or an argument for another command. You can use the eval command to substitute a command. Go through the examples to learn more about this:
Case 1: Command Substitution Without Option
To substitute a command without any option, you can use the eval command at the beginning. For example:
echo "\$(date)"
This command line considers $(date) as a string and returns it as $(date). Now if you want to get the date value instead while using the date command, you can use the following command line:
eval echo "\$(date)"
In the image above, you can see, the echo command returns the date command and the eval command evaluates the date command.
Case 2: Command Substitution With Option
To substitute a command with an option, you can use the eval command. For example, consider the following command with a long option:
previousMonth='date --date="1 month ago"'
The previousMonth variable stores the date command with such an option that will return the date exactly one month ago. Now if you type the following command to get the date:
$previousMonth
This shows an error. However, the following command line with the command eval, will do the job perfectly:
eval $previousMonth
This image shows that the eval command evaluates the date command along with its option --date="1 month ago"
. So, the eval command can be handy when dealing with commands that involve lengthy options.
3. Performing Arithmetic Operation Using “eval” Command
To substitute the numeric variables, you can use the eval command. For instance, define two numeric variables days and hours, and use these variables to calculate another variable totalHours, which represents the product of the days and hours variables:
days=7
hours=24
totalHours='expr $days \* $hours'
Now use the eval command to display the value of the totalHours variable:
eval $totalHours
In the above image, you can see that the eval command has substituted the numeric variable with its numerical value 168.
Note: You can also perform the arithmetic operation directly and display the result. For example, in this case, to get the product of the variables days and hours, you can use the following commands:
totalHours=$(($days*$hours))
echo $totalHours
4. Using “eval” Command for Shell Scripting
You can use the eval command in shell scripting. Let’s demonstrate this with an example. First of all, open the nano editor to write a script named welcomeNote.sh:
nano welcomeNote.sh
Here’s an example of how to use the eval command for shell scripting:
#!/bin/bash
note='echo "Hello! This line is from the script."'
eval $note
This script stores an echo command in a variable named note, and uses the eval command to execute the echo command.
Now execute the script welcomeNote.sh:
welcomeNote.sh
The image above contains the output of the script welcomeNote.sh. From the image, it is clear that the eval command has evaluated the variable note and worked properly in the script.
Alternatives to “eval” Command
You can use some alternatives to the eval command. For example:
Case 1: Using “source” Command as an Alternative to “eval” Command
As an alternative to the eval command, you can use the source command. Type the following command line in the terminal to write a command in a script named welcome:
echo 'echo Hello! This line is from the script.' > welcome.sh
Now, use the source command to run the command from the script “welcome.sh”:
source welcome.sh
The image shows that the source command executes the command of the welcome.sh script directly in the terminal.
Case 2: Using Variable Substitution as an Alternative to “eval” Command
If you are working on a relatively small project, you may consider directly using the variable substitution rather than using the eval command. For example, store a command in a variable named “today”:
today=$(echo 'date')
Now directly use the variable substitution to get the date:
$today
In the above image, you can see, the command stored in the variable today is substituted.
Conclusion
In this article, I’ve demonstrated the eval command in Linux with some suitable examples. It is useful for substituting variables, commands, long options, and so on. Try out the useful examples yourself to get a hold of the eval command. I hope this article will be useful to you for that.
People Also Ask
Why use eval in bash?
You can use the eval command in bash for various reasons. This includes the execution of other commands, substituting variables, and so on.
What is the difference between eval and exec in Linux?
The difference between the eval and exec commands is that the eval command evaluates and executes shell commands, while the exec command replaces the current shell process with a new command. The eval command passes its arguments as input to the shell and executes them.
Why the eval command should be used with care?
The eval command evaluates and executes its arguments as shell commands. Now if the argument of the eval command is a malicious command, then the harmful command will be executed by the shell and cause harm to the system. Also, using the eval command excessively may pose difficulty in debugging. Hence, we should use the eval command with care.
Related Articles
- The “echo” Command in Linux [7 Practical Examples]
- The “alias” Command in Linux [3 Practical Examples]
- The “unalias” Command in Linux [3 Practical Examples]
- The “exit” Command in Linux [5 Practical Examples]
- The “bash” Command in Linux [5 Practical Examples]
- The “clear” Command in Linux [3 Practical Examples]
- The “declare” Command in Linux [7 Practical Examples]
- The “export” Command in Linux [8 Practical Examples]
- The “source” Command in Linux [3 Practical Examples]
- The “hash” Command in Linux [6 Practical Examples]
- The “read” Command in Linux [8 Practical Examples]
- The “set” Command in Linux [6 Practical Examples]
- The “unset” Command in Linux [2 Practical Examples]