The eval Command in Bash Script
 
This article is about using string as a command in a Bash script. For this purpose, the eval command is used.
Eval Command in Bash Script
There are certain Bash scripts where you must create a string using variables or input values (for example) and run it as a command at the end. For such cases, eval is the solution.
In Bash, the eval command evaluates parameters similar to a shell command. Arguments are combined into a string and sent to the shell command, which then executes the command.
In the current shell, eval runs the command. This command is convenient when running a command with a specific operator or reserved terms.
This command may also be used in any script where the variable name isn’t known until the script is run. This guide will assist Linux users in understanding how to use this command.
Syntax:
eval [arguments...]
The arguments are passed to this command combined as a string literal and then sent to Bash for execution. This command returns the exit status after the execution of the command.
If no arguments are passed to this command or null is passed, it returns 0 as the exit status.
Count Number of Lines in a File Using eval
Suppose we have a file sample.txt containing some lines of text. If we need to count the number of lines in that file, we can use the wc -l command.
We will perform this task using the eval command. The command will be:
myComm="wc -l sample.txt"
eval $myComm
Let’s look at the output of this.

Solve Arithmetic Operation Using eval
Suppose we need to perform some arithmetic operations. We can do this using the eval command.
We will create a Bash script for this purpose.
#!/bin/bash
a=4
b=5
comm1="`expr $a + $b`"
comm2="echo"
eval $comm2 $comm1
In this script, we created two variables containing two commands, one for performing arithmetic operations and the other for an echo command. In the last line, we have passed these two variables as arguments to the eval command.
eval will combine these two variables and construct a command like the below.
echo 'expr 4 + 5'
After the execution of this script, we will get the following output.

Print the Values of Variables Using eval
In the following example, we will print the value of a variable that contains another variable.
#!/bin/bash
str1="my script"
str2=str1
comm="echo"
eval $comm \${$str2}
The above script’s output will be:

Print the Sum of Numbers Using eval
In the following example, we will print the sum of numbers ranging from 1 to 4 using a for loop in the script. Then we will print that sum using the eval command.
The script for this problem is:
#!/bin/bash
sum=0
for n in {1..4}
do
sum=$(($sum+$n))
done
comm="echo 'The result of sum from 1-4 is: '"
eval $comm $sum
The above script’s output will be:

Conclusion
The eval command may run by expressing any Bash command as a string. In this lesson, the eval command runs several Bash built-in tasks and creates a sequence of variables.
After reading this lesson, users will have a better understanding of the eval command and will be able to utilize it for a variety of applications.
Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.
LinkedIn