After some unsuccesful attempts at quoting the command string correctly, I ended up using the below command to run the identify command on the .jpg files under the working directory, the names of which may contain single and/or double quotes:

for f in *.jpg; do
    xterm -e bash -c "identify -verbose \"${f/\"/\\\"}\" | less"

However, I realized that variable expansion with string replacement construct wasn't POSIX compatible.

So, what is your recommended way to quote such a command line argument that possibly expands to a string with single and double quotes, and what is a POSIX-compatible syntax for the above command?

  • How about letting the shell do the for loop: xterm -e bash -c 'for f in *.jpg; do identify -verbose "$f" | less; done' instead of opening a new xterm window every time.
    – user391836
    Feb 9, 2020 at 8:43
  • @thisaccounthasbeen Works well if you want info for all images in a single window, plus avoids the nested quotes problem, although I use the command in a plugin script, where I prefer the details of selected images in separate windows.
    – detic
    Feb 10, 2020 at 22:22

1 Answer 1


You could pass the filename as argument to the command string:

for f in *.jpg; do
  xterm -e bash -c 'identify -verbose "$1" | less' bash "$f"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.