Pau*_*ce. 102
不要使用ls,它不是为此目的.使用globbing.
shopt -s nullglob
array=(*)
array2=(file*)
array3=(dir/*)
Run Code Online (Sandbox Code Playgroud)
nullglob如果没有匹配项,该选项会导致数组为空.
anu*_*ava 19
以下将在当前目录中创建一个带ls输出的数组arr:
arr=( $(ls) )
Run Code Online (Sandbox Code Playgroud)
虽然使用输出ls完全不安全.
比ls你可以使用的更好,更安全echo *:
arr=( * )
echo ${#arr[@]} # will echo number of elements in array
echo "${arr[@]}" # will dump all elements of the array
Run Code Online (Sandbox Code Playgroud)