Skip to main content

Use eval Command in Linux

The eval command let's you execute arguments as a shell command. You can combine arguments into a single string, use the result as input to the shell and execute the resulting commands.

β€” Sagar Sharma

Warp Terminal

Suppose you want to execute multiple commands in Linux, sure you can chain them but what if I tell you that you can store them in a variable and execute it?

That's where the whole idea of eval command comes in by allowing you to execute the chain of commands stored in one variable.

So in this tutorial, I will walk you through the multiple examples of using the eval command in Linux.

How to use the eval command in Linux

To use the eval or any other command, it is always a good idea to start from the syntax. So here's the syntax of using the eval command:

eval [argument]

Here, in the [argument] field, you enter the variable which is assigned with a chain of commands.

In simple terms, you append the variable which contains the multiple commands with desired options.

Now, let's have a look at some examples of the eval command.

1. Evaluate multiple commands from variable

The first step is to create a variable in which you will store the chain of commands. To create a variable, use the following command syntax:

variable_name="chain of commands"

For example, here, I created a variable sagar that will print greeting text for a logged-in user:

sagar="echo 'Hello $USER'"

Once done, you can use the eval command in the following manner to execute the commands stored inside the variable:

eval $<variable_name>

As I named my variable sagar, I will be using the following:

eval $sagar
Use eval command to evaluate and execute commands from the variable

2. Use multiple variables

While for most users, using one variable would get the job done, you may want to use multiple variables for better control over execution.

In that case, you can simply merge them in the following manner:

eval $command_1 $command_2

But if you want to add any additional command such as echo or print, then you'd have to use double quotes around it:

eval "echo $command_1 $command_2"

For your reference, here, I have used two strings in $command_1 and $command_2 as shown here and later on used the echo command with the eval command:

Use multiple variables in eval command

3. Perform mathematical operations

To perform mathematical operations, the syntax is similar to what I explained to use multiple variables but with a little twist.

Let's suppose you have two variables: var1 and var2 holding numeric values and you want to add them (perform addition).

So you have to use the double parentheses ((...)) for that purpose.

Sounds complex? Let me show you how to pull that off.

I want to perform addition between var1 and var2, so the first step is the allocate them some numbers:


Once done, take another variable to show some text:

result="Your total is"

Finally, put it all together as shown here:

eval 'echo $result' $(($var1+var2))
Use the eval command to perform mathematical functions

More ways to execute multiple commands

If using the eval command to execute multiple commands does not sound a good deal to you then we wrote a detailed guide explaining how to run multiple commands in Linux:

Run Multiple Linux Commands in One Go
In this quick, beginner’s tip, learn to run multiple Linux commands one after another in a single command.

I hope you will find this helpful.

Sagar Sharma