Beginners Guide for Eval Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The eval command is used to execute specified arguments as a single command in the current command-line processing and return its result.

It will combine (or construct) the arguments into a single string and use it as input to the shell, which will execute the resulting commands in the current shell environment.

Tutorial Details

Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Internet RequiredNo
Discussed Tools in this Article

Syntax of the Eval Command

The eval command only takes one argument.

$ eval [ARG ...]

Evaluating the Variable Value as a Shell Command

If you have a variable or have created a new variable with a value, that is a valid Linux command.

Then you can evaluate that variable in your shell with a variable name passed to the eval command as an argument.

$ wd="cd /var/www/html"
$ eval $wd

Another example;

$ iam="whoami && id"
$ eval $iam
uid=1003(linuxtldr) gid=1004(linuxtldr) groups=1004(linuxtldr), 27(sudo), 1007(admin), 1008(newadmin)

If you specify the invalid Linux command to your variable, you will get the following error:

$ err="Wrong-Command"
$ eval $err
Wrong-Command: command not found

Creating a Variable with the Eval Command

You know the Bash way of creating variables in “[KEY=VALUE]” format. However, you can use the eval command to create the same variable as shown.

$ eval "wd='cd /var/www/html'"
$ echo $wd
cd /var/www/html

Another example;

$ eval "iam='whoami && id'"
$ echo $iam
whoami && id

There is no difference between this and the standard way of creating variables; only in this case, you are declaring the variable as an argument to the eval command.

Instead of echoing the variable, if you evaluate it using the eval command, the specified value as a string will be sent to shell, and you will get the resulting output.

$ eval "wd='cd /var/www/html'"
$ eval $wd

Counting the Content of Text Using Eval

If you have a variable that counts the number of lines in the text file using the wc command.

$ command="wc -l file.txt"

The following is the content of the “file.txt” file.

line one
line two
line three
line four

Then you can use the variable name “command” as an argument to “eval command” to execute its value as a shell command.

$ eval $command
4 file.txt

Creating a Series of Variables and Evaluating Them

You can create multiple variables and evaluate them together in your shell.

For example, create two variables with the names “x” and “y” and assign them the values “6” and “6“, then create another variable with the name “msg” and assign it the value “Your total is“.

Now you can use these variables as an eval argument to evaluate them and return the result.

$ x=6
$ y=6
$ msg="You total is"
$ eval 'echo $msg' $(($x+$y))
You total is 12

Double Evaluating the Variables

With the eval command, you can assign a variable name to another variable and then evaluate the value of the parent variable using the new variable.

For example, if you define “var1” with the “I am variable” value and “var2” with the “var1” value.

Than create variable “var3” with string “$” within the single quotes “β€˜” and variable name “var2” and then check the result for “var3” variable using the echo command.

$ var1="I am variable" var2=var1
$ var3='$'var2
$ echo $var3

In the above command, the “var3” value is the string “$var2“.

Now instead of echoing if you evaluate the variable “var3” with its own name, it will first evaluate “$var2” and assign its “var1” value to its own variable “var3“.

$ eval var3=$var3
$ echo $var3

Now, if you specify the “$” within the single quotes “β€˜” with “$var3” as its own variable, it will evaluate the “var3” existing variable that is “var1” and assign its “I am variable” value to “var3“.

$ eval var3='$'$var3
$ echo $var3
I am variable

This example might be tricky to understand, for instance, but you can walk through it over and over to understand.

That was the last example.

I think the eval command is a little tricky for beginners, but it is a very simple command, and I tried to provide sensible examples to make this article effective for beginners.

I suggest you share your uses of eval in the comment section so I can include them in this article.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.