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

后端 未结 8 1481
渐次进展
渐次进展 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:37

    For folks (like me) finding this through Google, @Peter and @feoh's answers are the general solution to "How to glob variables in bash script".

    list_of_results=(pattern)
    

    will save existing filenames matching pattern into the array list_of_results. Each element of list_of_results will hold one filename, spaces and all.

    You can access each result as "${list_of_results[]}" for starting from 0. You can get the entire list, properly quoted, as "${list_of_results[@]}".

提交回复
热议问题