single="ls -l"
$single
=============
multi="ls -l | grep e"
echo $multi > tmp.sh
bash tmp.sh
=============
cmd="ls -l | grep e"
bash -c "$cmd"
=============
$ cmd="echo foo{bar,baz}"
$ $cmd
foo{bar,baz}
$ eval "$cmd"
foobar foobaz
=============
=============
eval "$1"
Executes the command in the current script. It can set and use shell variables from the current script, set environment variables for the current script, set and use Functions from the current script, set the current directory, Umask, limits and other attributes for the current script, a nd so on. bash "$1"
executes the command in a completely separate script, which inherits environment variables, file descriptors and oth ER process environment (but does does transmit any change back) but does not inherit internal shell settings (shell variabl ES, functions, options, traps, etc.).
There is another to, (eval "$1")
, which executes the command in a subshell:it inherits everything from the calling script but does not transmit any.
For example, assuming that the variable dir
isn ' t exported $1
cd "$foo"; ls
and are, then:
cd /starting/directory; foo=/somewhere/else; eval "$1"; pwd
Lists the content of and /somewhere/else
prints /somewhere/else
.
cd /starting/directory; foo=/somewhere/else; (eval "$1"); pwd
Lists the content of and /somewhere/else
prints /starting/directory
.
cd /starting/directory; foo=/somewhere/else; bash -c "$1"; pwd
Lists the content of /starting/directory
(because cd ""
doesn ' t change the current directory) and prints /starting/directory
.
=============
Note: The script file must be in UNIX format; there are no blank lines in the script file, no comments;
cat/studies.sh | While the read line does while [1-eq 1] do declare-i filelines filelines= ' qstat-u hy | grep LQ | Sed-n ' $= ' echo-e "$fileLines \c" if (($fileLines <50) then echo "$line" eval "$line" pwd Break else sleep + fi done
REF:
Http://stackoverflow.com/questions/3469705/how-to-run-script-commands-from-variables
Http://stackoverflow.com/questions/4668640/how-to-execute-command-stored-in-a-variable
Http://unix.stackexchange.com/questions/124590/variable-as-command-eval-vs-bash-c
Http://superuser.com/questions/679958/execute-a-command-stored-in-a-variable
Http://www.cyberciti.biz/tips/howto-running-commands-from-a-variable.html
Http://www.tldp.org/LDP/abs/html/commandsub.html
http://ubuntuforums.org/showthread.php?t=1018434
Naming scripts in Linux run variables