What is the difference between (*)
and ("$(ls)")
?
Are they essentially the same except the delimiters are different?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityWhat 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
("$(ls)")
, that involves parsing the output of ls
generally considered to be an abomination.