What is the difference between (*) and ("$(ls)")?
Are they essentially the same except the delimiters are different?
What is the difference between (*) and ("$(ls)")?
Are they essentially the same except the delimiters are different?
The first one, (*), globs the list of files and directories in the current directory and creates a list. You can assign that list to an array variable, and each file name will be its own entry.
touch 'a b' c
d=(*)
printf "> %s <\n" "${d[@]}"
> a b <
> c <
The second one, (“$(ls)”), invokes ls to list the current directory. The resulting list of files and directories is put into a single string and assigned to a list. The list contains this single element consisting of the newline-separated set of names.
d=("$(ls)")
printf "> %s <\n" "${d[@]}"
> a b
c <
The first one is better as the file names are posted properly into individual elements of the list, and parsing the output of ls is often fraught with unexpected complications