Escaping spaces and shell scripts

MacUser
2012-07-01
2013-04-29
  • MacUser
    MacUser
    2012-07-01

    Hi,

    I'm writing some shell scripts and have run into a problem whereby certain options refuse to honour double quotes around spaces. I've tried literally everything, including the "backticked cat".

    Is there a better way or workaround?

     
  • Smiling Dragon
    Smiling Dragon
    2013-01-04

    I've not hit any issues getting it to work with spaces in filenames in my scripts.
    Just watch out for "for loops" (they *hate* spaces) and always remember to quote your variables
    Code excerpt:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    #!/bin/sh
    while [ -n "$1" ]
    do
            filename="$1"
            shift
            ...bunch of filename munging to figure out episode names etc...
            AtomicParsley "${filename}" --stik "TV Show" --overWrite \
            --TVShowName "${showname}" --sortOrder TVShowName "${sortshowname}" \
            --title "${title}" --description "${title}" \
            --TVEpisode "${title}" --sortOrder TVEpisode "${filename}" \
            --TVSeasonNum "${season}" --TVEpisodeNum "${episode}"
    done
    
    ./my-code.sh "some file with spaces.mp4" "some other file.mp4"
    or
    ./my-code.sh *.mp4
    Or, if I've totally missed your point and you are doing something else, try pasting the code you have that's exploding and someone might spot the issue.
    
     
  • MacUser
    MacUser
    2013-01-13

    Thanks for the reply smiling dragon. I think you have correctly interpreted what I was trying to get across but I can still reproduce the problem, so here's a very simple example that should illustrate:

    1
    2
    3
    4
    5
    6
    7
    #!/bin/sh
    title1="Some Episode Name"
    show="Some Show Name"
    season="1"
    TVShowName="$show"
    fileData=""$show" --sortOrder TVShowName "$TVShowName"  --purchaseDate "timestamp""
    AtomicParsley "somefile.mp4" --title "$title1" $fileData
    

    Any suggestions as to how to escape a space inside a substituted variable?

    Thanks again.