I have a $variable
that has many double quoted paths separated by spaces
echo $variable
"/home/myuser/example of name with spaces" "/home/myuser/another example with spaces/myfile"
The number of paths on my variable can vary and it's not something under control. It can e.g. be like the following examples:
example 1: "path1" "path2" "path3" "path4"
example 2: "path1" "path2" "path3" "path4" "path5" "path6" path7" "path8"
example 3: "path1" "path2" "path3"
example 4: "path1" "path2" "path3" "path4" "path5" "path6"
I want to replace all spaces outside the double quotes into a new line (\n
) while preserving the spaces that are inside quotes. Using echo $variable | tr " " "\n"
like in this answer doesn't work for me because it replaces all the spaces by new lines. How can I do it?
printf "%s\n" "${variable[@]}"
– jasonwryan Jun 29 '18 at 00:33variable="\"path1\" \"path2\" \"path3\""
... But now I see that it needs to be declared as an array to make it work with the printf. – Rafael Muynarsk Jun 29 '18 at 01:21