I am trying to take a filename from the ~/Pictures
folder and supply it as an argument for the nomacs
command. The filename contains some spaces, so I am using Bash substitution to escape spaces (I also want to take last file in the folder).
The code:
names=$(\ls ~/Pictures * | tac)
SAVEIFS=$IFS
IFS=$'\n'
names=($names)
IFS=$SAVEIFS
screenshot=~/Pictures/${names[0]}
screenshot=${screenshot// /\\ }
nomacs $screenshot
Example of the filename: Screenshot from 2017-09-13 18-05-42.png
The problem is that nomacs $screenshot
does not work but when I execute nomacs Screenshot\ from\ 2017-09-13 18-05-42.png
, it works as expected.
Should I use some special Bash technique for escaping spaces?
ls
. You should probably look into either usingfind
or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here. Further, what specifically do you mean by 'last' file in the directory? Most recent? Last lexically? Last in character sort order? Last modified? What if this 'last' file is a directory? – DopeGhoti Sep 13 '17 at 16:33ls
, which is a bad idea that should make you feel bad. – DopeGhoti Sep 13 '17 at 16:36ls ~/Pictures/*
there? Otherwise you're getting an ls of ~/Pictures and of * – Jeff Schaller Sep 13 '17 at 16:39