I made a script to automate ffmpeg conversion from mkv > m4v > open the m4v in Subler.app to import the metadata manually, at which point I'll move the file into my iTunes library.

I use iTunes because I have two Apple TVs on the network and use a 2007 iMac to function as my media server. I locally ssh into this machine in order to execute this script.

This script fails if I enter a path with whitespace(s), or enter title for the movie that includes whitespace. How do I fix this script to accommodate whitespace? I know the syntax is sloppy, I’m very new to scripting, so any other pointers to make it run better would be appreciated :)

Here it is:

    #! /bin/bash


function convert {
    ffmpeg -i $REPLY -strict -2 -c:v copy -c:a copy -c:s copy $PWD/out.m4v

function rename {
    echo "Please Provide the Title:"
    read -e; mv $PWD/out.mp4 $PWD/$REPLY.m4v

function subler {
    open -a Subler.app $REPLY.m4v

cd ~/Public/ &&
while read -e; do
    if [ ${REPLY: -4} == ".mkv" ]; 
    then convert;
    echo "Error: This is not a valid response" && exit 1



Question may still be a possible duplicate, but I’ve tried to use the syntax (both attempting “$REPLY” and “$(REPLY)”) in the linked answer and still failing to execute. The errors return either bad substitution for the […bracketed...] statement or my echo statement... Error:This is not a valid response…

  • The short answer is to put references to shell variables in double quotes. Thus, replace $REPLY with "$REPLY" and $PWD` with "$PWD" etc. There are many tutorials that discuss shell quoting issues.
    – John1024
    May 15 '16 at 5:43
  • @John1024 Thanks for the reply. I’ve modified the variables as the possible duplicate and your comment suggests…still getting error. I suspect that if [ ${REPLY: -4} == ".mkv” ] is the issue. Perhaps brackets requires a different syntax? I’ve tried if [ "${REPLY: -4}" == ".mkv” ], if [ $"({REPLY: -4})”….
    – njboot
    May 15 '16 at 5:53
  • The correct format is [ "${REPLY: -4}" = ".mkv” ] and, if that gives you a bad substitution error message, then you are not running the script under bash. Run the script as bash scriptname, not sh scriptname.
    – John1024
    May 15 '16 at 6:28
  • @John1024 That’s the syntax I used in the original script before modifying anything. Adding double quotes around the “$REPLY” variable in the other lines still returns error w/ whitespace.
    – njboot
    May 15 '16 at 6:31
  • You cannot use typographic quotes in Bash - you have to use " and '.
    – l0b0
    May 15 '16 at 9:40

So, guidance + trial and error led me to solution. I quoted the “$REPLY” variable as suggested…but I also needed to [[…]] instead of […] for the script to execute properly. I’m not exactly sure why, but now it’s working nonetheless. Thank you SE community!

  • 1
    You might want to check out shellcheck.net, it would have pointed out to you that you need to quote the left side in […] calls (it's basically as if you had called test ${REPLY: -4} = ".mkv", if you don't quote then it's going to be split before the =).
    – phk
    May 15 '16 at 9:48

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