Take it step by step:
When you do this:
mycmd='cat $myfile'
You prevent shell interpolation of $myfile . Thus:
$ echo $mycmd cat $myfile
If you want to enable interpolation, you can use double quotes:
$ mycmd="echo $myfile"
This, of course, freezes the interpretation of $mycmd when you do eval .
$ myfile="afile.txt" $ mycmd="echo $myfile" $ echo $mycmd cat afile.txt $ eval $mycmd
Compare this to:
$ myfile="afile.txt" $ mycmd='cat $myfile'
What you probably want to do is evaluate $mycmd in an echo expression when you echo it:
$ echo $(eval "echo $mycmd") $ cat afile.txt $ myfile=bfile.txt $ echo $(eval "echo $mycmd") cat bfile.txt
David W.
source share