linux eval code example

Example 1: linux eval

~$ CD="cd Desktop" 
~$ eval $CD  # Implements the command in a string ( $ eval "cd Desktop")
~/Desktop$

Example 2: eval function in unix

eval [arg ...]
    The  args  are read and concatenated together into a single com-
    mand.  This command is then read and executed by the shell,  and
    its  exit status is returned as the value of eval.  If there are
    no args, or only null arguments, eval returns 0.

Example 3: eval bash

Just use eval command for running string output. As for example:
eval $cmd   #execute command included in string value of $cmd variable
Or for a more sophisticated use run commands stored in lines of a file:
cat commands_file.txt | while read line; do eval $line; done

Tags:

Misc Example