Javatpoint Logo
Javatpoint Logo

Shell Scripting eval command

The eval command is a built-in command. It takes a string as its argument and evaluate it, then run the command stored in the argument. It allows using the value of a variable as a variable.

Example 1:

Linux Shell Scripting eval 1

Look at the above snapshot, command "echo \${$User}" runs $User as a shell variable and displays its output.

But command "eval echo \${$User}" runs the parameter passed to eval. After expansion, remaining parameters are echo and ${Hello}. Hence eval command runs the command echo ${Hello}. And so the output is Mr. X

Double quotes must be used around variable and command substitution. Without the double quotes, shell may perform field splitting on different words of the variable.

Linux Shell Scripting eval 2

Example 2:

Linux Shell Scripting eval 3

Look at the above snapshot, we have passed a parameter (1 week ago) to the date command. This is the last week date and time displayed.

But when we set this command in a variable (lastweek) and run it, the command fails to print the date. Look down,

Linux Shell Scripting eval 4

Look at the above snapshot, command "$lastweek" fails whereas, command "eval $lastweek" successfully runs.

(( ))

This sign is mainly used for numerical evaluation. It is a compound command.

Linux Shell Scripting eval 5

Look at the above snapshot, single bracket ( ) gives error while double bracket (( )) successfully executes the command.

Next TopicScripting let

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA