I tried to create a script to list the contents of a directory:
#!/bin/bash
matched=$(ls -1 /data/ | grep $1)
echo $matched
I have added the parameter -1 to the ls command and when executed like this ./script dir the output is on one row:
dir1 dir2
I’ve also tried echo -e $matched, but the output was:
-e dir1 dir2
So how can I list the directories each on separate line ?
Try using double quotes around the string to be echoed:
The quotes here cause certain special characters to be preserved; see here.
Edit: See cdarke’s comment for a better explanation.