Is there an easy way to set nullglob for one glob

前端 未结 5 1085
谎友^
谎友^ 2021-02-03 23:22

In bash, if you do this:

mkdir /tmp/empty
array=(/tmp/empty/*)

you find that array now has one element, \"/tmp/empty/*\"

5条回答
  •  再見小時候
    2021-02-04 00:20

    This may be close to what you want; as is, it requires executing a command to expand the glob.

    $ ls
    file1 file2
    $ array=( $(shopt -s nullglob; ls foo*) )
    $ ls foo*
    ls: foo*: No such file or directory
    $ echo ${array[*]}
    file1 file2
    

    Instead of setting array in the subshell, we create a subshell using $() whose output is captured by array.

提交回复
热议问题