Eval Command in Linux

Eval is built in unix command – it’s used to execute arguments as shell commands. It’s useful when you have command stored in variable and you want to execute it.

[root@server Files]# mycommand="ls -ltr"

[root@server Files]# echo $mycommand
ls -ltr

[root@server Files]# eval $mycommand
total 0
-rw-r--r-- 1 root root 0 Nov  5 07:40 file1.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file3.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file2.txt
[root@server Files]#

Look at the example above, I have stored the command ls – ltr in the variable mycommand so when I do echo it’s just prints what in the variable. But when I do eval followed with the variable – it will actually execute the command.

Eval is powerful command but it’s Evil – it has security issues as well.
You can find the details here

Leave a Reply

Your email address will not be published. Required fields are marked *