When I save a command output which contains several lines to a variable directly into my terminal, I have the following results :
$ dirs=$(ls -1d /mnt/*/)
$ echo $dirs
/mnt/ext4/ /mnt/local/ /mnt/remote/ /mnt/test/
$ echo "$dirs"
/mnt/ext4/
/mnt/local/
/mnt/remote/
/mnt/test/
However when using it from a posix shell script the result is different. Here is the script
#!/bin/sh
dirs=$(ls -1d $1)
echo "inline"
echo $dirs
echo "multiline"
echo "$dirs"
And here is the script output
$ ./test.sh /mnt/*/
inline
/mnt/ext4/
multiline
/mnt/ext4/
This happens even if I use bash instead of sh.
Does anyone know how I could save the output of ls -1d /mnt/*/
into a variable keeping the full output ?
I would like to parse each of the four directory paths inside a for loop.