Unix Powerful Command Series – Eval Command and Usage

The eval command is one of the most powerful and flexible commands in Linux. It combines all the given arguments and evaluates the combined expression and executes it, and returns the exit status of the executed command.

This command is commonly used to execute arguments as a shell command on Linux system. Eval in <bash> works in essentially the same way as in most other languages that have an eval function. Perhaps the easiest way to think about eval command is that it acts in the same manner as running the <bash -c “bash code…”> from a script.

In the below section, you’ll see a real-time example of the eval command. But you should understand that the Linux OS provides numerous commands which can help you in automating a lot of your tasks.

If you wish to know about all these essential Linux commands, then do follow this post, “Essential Linux Commands for Beginners.“.

The Eval Command Syntax.

eval is useful when a command contains something which needs to be evaluated by the shell. The typical case would be when you have a variable containing the index of an argument you want to access.

eval: eval [arg ...]
    Execute arguments as a shell command.
Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.
Exit Status:
    Returns exit status of command or success if command is null.

 

Watch eval command in action.

Let’s now begin with a real-time use case of the eval command to fully explain its usage. It’ll also let us understand the point that how can we use it to fix our problem scenario.

So, in our use case, there is a set of three variables say DIR, Username (UN) and TIME. Each of these variables is postfixed by the index number. And there can be N number of such sets.

The challenge is to perform the following operations.

1. Enumerate through all these sets in a loop.
2. Dynamically postfix the index no to these variables.
3. Evaluate their actual values at run-time.
4. Print the actual value of the evaluated variable.

#!/bin/bash
#Used bash shell to support for loop syntax used below

#The no of times you want to iterate
count=3

#add any no of variables you wish to print in loop
DIR1=/home/a
UN1=Harsh1
Time1=10

DIR2=/home/b
UN2=Harsh2
Time2=20

DIR3=/home/c
UN3=Harsh3
Time3=30

echo
#for loop to run based on count variable
for (( i=1; i<=$count; i++ ))
do
  echo $(eval echo '$'$(eval echo DIR$i))
  echo $(eval echo '$'$(eval echo UN$i))
  echo $(eval echo '$'$(eval echo Time$i))
  echo
done

 

Final thought.

We hope the information about eval command would be beneficial for you. The mere objective of writing about this Shell command was to make you aware of the sheer power of its features. So that you also use it with ease in your shell scripts. If you have a better example of this command, please do share with us by leaving your comments in the comment box section.

Thanks to all of you for reading this article. We’ll continue delivering shell scripting tutorials to make you master the art of scripting.

If this post succeeds in drawing your interest, then help others by sharing it. 🙂

Best,

TechBeamers.

2 Comments

  1. meenakshi404 Reply
    • Harsh S. Reply