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 and Experienced.“.

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.


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 post-fixed 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 post-fix the index no to these variables.
3. Evaluate their actual values at run-time.
4. Print the actual value of evaluated variable.


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 any 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. 🙂





Leave a Reply