How to assign a glob expression to a variable in a Bash script?

后端 未结 8 1455
渐次进展
渐次进展 2020-12-08 02:21

When the following two lines of code are executed in a bash script, \"ls\" complains that the files don\'t exist:

dirs=/content/{dev01,dev02}
ls -l $dirs


        
相关标签:
8条回答
  • 2020-12-08 02:53

    Here is an excellent discussion of what you are trying to do.

    The short answer is that you want an array:

    dirs=(/content/{dev01,dev01})
    

    But what you do with the results can get more complex than what you were aiming for I think.

    0 讨论(0)
  • 2020-12-08 02:56

    The issue which no-one has addressed is that the variable assignment makes the difference.

    dirs=/content/{dev01,dev02}
    

    expands differently than

    echo /content/{dev01,dev02}
    

    The question is how to assign the results of the expansion to dirs

    See: How to use Bash substitution in a variable declaration

    0 讨论(0)
提交回复
热议问题