Reading filenames into an array

前端 未结 5 2102
青春惊慌失措
青春惊慌失措 2020-11-29 18:30

I want to get a list of files and then read the results into an array where each array element corresponds to a file name. Is this possible?

5条回答
  •  执笔经年
    2020-11-29 19:17

    Don't use ls, it's not intended for this purpose. Use globbing.

    shopt -s nullglob
    array=(*)
    array2=(file*)
    array3=(dir/*)
    

    The nullglob option causes the array to be empty if there are no matches.

提交回复
热议问题