Beginners Guide for Eval Command on 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
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Internet RequiredNo

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


Evaluating variable value as shell command using eval

Another example;

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


Another example of evaluating variable value as shell command using eval

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

$ err="Wrong-Command"
$ eval $err


Running variable assigned to wrong command

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


Creating shell variable using eval command

Another example;

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


Another example of creating shell variable using eval command

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


Evaluating command assigned as variable using eval command

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


Evaluating file with eval command

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))


Creating variable and then evaluating them using eval command

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


variable swaping

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


Variable reassigning

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


var3 variable value reassinged

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.