eval Command in Linux



eval is a Linux command that is used to execute a string or argument as a shell command. It takes the input argument, processes it and runs it within the current shell environment. The eval command is pretty helpful for dynamically constructing and executing commands stored in variables. It allows for more flexible and powerful scripting since it enables the execution of complex command structures.

With eval command, you can automate tasks and handle commands that need to be generated and executed on the fly.

Table of Contents

Here is a comprehensive guide to the options available with the eval command −

Syntax of eval Command

The basic syntax to use the eval command on Linux is given below −

eval [arguments]

Here, [arguments] are the strings or variables that contain the commands you want to execute.

Basic Use of eval Command

The basic use of the eval command is to take input strings as arguments, concatenates these arguments into a single string, then parses it as a shell command, and executes it after that. For example, consider a scenario where we have a variable containing a command 'ls -l' that needs to be run. Instead of manually typing out the command each time, eval can take the variable, interpret it as a command, and execute it.

cmd = "ls -l"
eval $cmd
Basic Use of eval Command in Linux

Advanced Use of eval Command

Once you are done with the basic use of the eval command, it’s now time to explore some more advanced usage of the eval command. This includes using eval commands with variables and command substitution. However, before you put yourself into these examples, it is better to get an understanding about some command-line arguments that can be used with the eval command. These are provided below in the table −

Argument Description Example Output
$$ Process ID of the current shell eval echo $$ 11536
$? Exit status of the last command executed eval echo $? 0
$0 The name of the command you’re running eval echo $0 bash
$# The number of arguments supplied to a script eval echo $# 0
$1 to $9 The first 9 additional parameters to the shell script eval echo $1 No result
$* All the arguments are double quoted eval echo $* No result
$@ All the arguments are individually double quoted eval echo $@ No result

Examples of eval Command in Linux

Now, let’s explore some advanced examples of eval command on Linux system −

  • Storing and Executing Commands from Variables
  • Command Substitution
  • Accessing Variables within Variables
  • Performing Arithmetic Operation
  • Array Manipulation

Storing and Executing Commands from Variables

You can store a command in a variable and then execute it using the eval command on Linux. This is useful for dynamically generating commands. For example −

directory="/tmp"
list_command="ls -l $directory"
eval $list_command

The above process sets up the directory to /tmp, creates the list_command to list its contents, and then executes that command to get detailed information of the /tmp directory.

Storing and Executing Commands from Variables

Command Substitution

You can also use the eval command on Linux to execute the command’s output in a variable. For example −

current_date="echo \$(date)"
eval $current_date

The above process stores the command to get the current date in the current_date variable and then executes it to display the current date and time.

Command Substitution 1

Accessing Variables within Variables

The eval command can also be used in Linux to access the value of a variable that is stored in another variable. For example −

var1="Hello"
var2="var1"
eval echo \$$var2

Here, var1 contains the string “Hello”, and var2 contains the name of the variable var1. Using eval, you can access the value of var1 through var2.

Command Substitution 2

Performing Arithmetic Operations

If you want to perform arithmetic operations on Linux, you can utilize the eval command to do that. For example −

num1=10
num2=5
operation="num1 + num2"
eval "result=$(( $operation ))"
echo "The result is: $result"

The above example sets up two numbers, num1 and num2, and an arithmetic operation. The eval command is used to perform the addition and stores the result in the result variable, which is then printed.

Performing Arithmetic Operations

Arrays Manipulation

You can also manipulate the array dynamically by using the eval command on Linux. For example −

array_name="my_array"
eval "$array_name=(1 2 3 4 5)"
eval "echo \${$array_name[2]}"

The above example sets up an array named my_array with elements 1, 2, 3, 4, 5. After that, it uses the eval command to access and print the third element of the array (index 2), which is 3.

Arrays Manipulation

That’s how you can use the eval command on your Linux system to perform different operations.

Conclusion

The eval command is a powerful tool in the Linux system used for executing strings or arguments as shell commands. In this guide, we have provided the syntax of eval command, along with basic and advanced usage through examples.

By mastering the eval command, you can significantly enhance the flexibility and power of your shell scripts. Further, it also helps you make them more dynamic and capable of handling complex command structures.

Advertisements