bashevalvariable-assignmentcommand-substitutionbrace-expansion

Expansion in an eval variable


Is it possible to do expansion inside an eval variable?

Example:

$ eval VAR=`echo some/path/{file1,file2}`
bash: some/path/file2: No such file or directory

I expected the same result as without using eval, that is, setting variable VAR to some/path/file1 some/path/file2


Solution

  • Adding quotation marks solved the problem:

    $ eval VAR=\"`echo some/path/{file1,file2}`\"
    

    Background: I was trying to define a variable inside eval which contained multiple lines of commands written in a PKGBUILD script, but was advised against doing that. For an appropriate solution, see Using variables in bash function names.