escaping metacharacters in paths for a shell command

I have a file which contains a list of paths separated by a new line character.

e.g

/some/path/to/a/file.png
/some/path to/another/file.jpeg
/some path/to yet/another/file

Notice that these paths may contain metacharacters, the spaces for example are also not escaped.

If I wanted to use this list in a command which allows multiple paths as input what would be the best way to do this?

for example the command

montage "/some/path/to/a/file.png" "/some/path to/another/file.jpeg" "/some path/to yet/another/file"

I thought about using awk to replace the newline characters with quotation marks. Is there a nice clean way of doing this.

Hello cue
You may use sed and tr for achieving this goal.
Try something like:

sed 's/^/"/ ; s/$/"/' file.txt  | tr "\n" " " > newfile.txt

By doing that, newfile.txt has all the paths from file.txt which are escaped by quotation marks and separated by a whitespace.
Hope it helps.

1 Like

use

sed 's/^/"/ ; s/$/"/' file.txt  | tr -d "\n"  > newfile.txt
1 Like

thanks pandeesh and chapeupreto. Can I also ask, what is the best way of then using that newly created file list in a command?

ImageMagick � View topic - montage list of images
would that read the list automatically from the newly formatted file?

Alternatively, try:

oldIFS=$IFS IFS="
"
montage $(cat file.txt)
IFS=$oldIFS

Or modify you script so that is can also read an input file with a command line option...

Maybe you could do the following:

files=$(< newfile.txt)
montage "$files"