Using the eval command in Linux to run variables as commands

0

There are probably many Linux users who have never encountered the rating ordered. In fact, it’s not really a “command”, but a builtin bash intended to treat the value of a variable as a command. For example, if you set up a variable that includes the command to display the current time in Sydney, Australia, it would probably look like this:

$dt=”TZ=date ‘Australia/Sydney'”

You can then run it like this:

$eval $dt

Thu 7 Jul 06:32:14 AEST 2022

This can save you from having to memorize the Date command syntax and specifying a time zone, but let’s take a closer look rating to see what else it can do for you.

The bash manpage will tell you a bit more about the rating command, although you have to scroll almost 5,000 lines to find it. There is, after all, no manual page for the built-ins. Here is what he would tell you:

Arguments are read and concatenated into a single component.

mand. This command is then read and executed by the shell, and

its exit status is returned as the value of eval. If there is

no arguments, or only null arguments, eval returns 0.

Of course, for anything you do by typing commands in the terminal window, rating isn’t likely to save you much time or effort. You could, after all, create an alias to do the same job for you. Here’s what it would look like:

$alias dt=”TZ=date ‘Australia/Sydney'”

$dt

Thu 7 Jul 06:37:31 AEST 2022

Save this alias in your .bashrc file and you can easily check what time it is in Sydney whenever you feel the need.

However, when creating a complex multi-part command, especially in scripts that need to collect the necessary data as they run, use rating executing the command can make executing the command much easier.

For example, while working on a recent script, I needed to find five-letter words, specific letters in known positions, and other specific letters in uncertain positions. I also knew that a number of letters were not included in the words I had to find. Step by step, the script built a long chain of grep commands to iterate through the word file to find only words that match my specifications. Here is an example of what such a command might look like with all its grep (match) and sound grep -v (ignore) commands and match found:

$ grep ^ch..t$ /usr/share/dict/words | eval grep -v ‘q’ | grep -v ‘w’ | grep -v ‘r’ | grep -v ‘y’ | grep -v ‘u’ | grep -v ‘I’ | grep -v ‘o’ | grep -v ‘p’ | grep -v ‘s’ | grep -v ‘d’ | grep -v ‘f’ | grep -v ‘g’ | grep -v ‘i’ | grep -v ‘k’ | grep -v ‘l’ | grep -v ‘z’ | grep -v ‘x’ | grep -v ‘v’ | grep -v ‘b’ | grep -v ‘n’ | grep -v ‘m’ | grep a | grep e | grep -v ..a.. | grep -v …e.

Cheat

In fact, if you run this command on your word file, you should get the same response. If you turn it into a variable as shown below, it will work the same.

$ cmd=”grep ^ch..t$ /usr/share/dict/words | grep -v ‘q’ | grep -v ‘w’ | grep -v ‘r’ | grep -v ‘y’ | grep – v ‘u’ | grep -v ‘i’ | grep -v ‘o’ | grep -v ‘p’ | grep -v ‘s’ | grep -v ‘d’ | grep -v ‘f’ | grep -v ‘g’ | grep -v ‘j’ | grep -v ‘k’ | grep -v ‘l’ | grep -v ‘z’ | grep -v ‘x’ | grep -v ‘v’ | grep -v ‘ b’ | grep -v ‘n’ | grep -v ‘m’ | grep a | grep e | grep -v ..a.. | grep -v …e.”

$eval $cmd

Cheat

My script uses rating to run the $ cmd variable as a command and passes the output to the column command that makes the output more useful when there are tens or hundreds of matching words. The result in the example shown was the single “cheat” word pulled from nearly half a million lines in the word file. All I had to do was answer a few questions about what I knew and what I didn’t know.

eval $cmd | column

Wrap

The rating The command can be used to execute simple or very complex commands which are saved as variables. It evaluates commands and variable arguments and then executes them. This is particularly useful when constructing long and complicated commands, especially in scripts where the details depend on data changing or responses provided as the scripts run.

UN.

Join the Network World communities on Facebook and LinkedIn to comment on the topics that matter to you.

Copyright © 2022 IDG Communications, Inc.

Share.

Comments are closed.