Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In my Bash-script i have to handle filenames with spaces. These are the important lines inside my script:

mp3file="/media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\ Vol.5\ \[MP3PRO\]/01\ -\ Nofx\ -\ Pump\ up\ the\ Valium.mp3"
echo "Command: mp3info -x `echo $mp3file`"
mp3info -x `echo $mp3file`

Unfortunately, the command does not work, because the filename is splitted:

mp3info: invalid option -- '\'
mp3info: invalid option -- '\'
Error opening MP3: /media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\: No such file or directory
Error opening MP3: Vol.5\: No such file or directory
Error opening MP3: \[MP3PRO\]/01\: No such file or directory
Error opening MP3: Nofx\: No such file or directory
Error opening MP3: Pump\: No such file or directory
Error opening MP3: up\: No such file or directory
Error opening MP3: the\: No such file or directory
Error opening MP3: Valium.mp3: No such file or directory

I also tried to add a custom IFS as i read on some forums:

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# Script like above
IFS=$SAVEIFS

But this way, i'm getting the error

Error opening MP3: /media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\ Vol.5\ \[MP3PRO\]/01\ -\ Nofx\ -\ Pump\ up\ the\ Valium.mp3: No such file or directory

I tried quite a while now but i cannot get my script to work. What is strange is that if i'm running the same command that my script should create manually (echoing it inside my script) on the Shell, it actually works. But not inside my script. Any hints?

share|improve this question
1  
I don't know what a backspace in IFS is supposed to do (never mind about the backspace, I just realized it could be any character and it's there to preserve the newline which would otherwise get dropped), but you can set IFS without using echo: IFS=$'\n'. Also, in one place you're using backticks and another you're using $(). The $() form is preferred since it's more readable, can be easily nested, etc. –  Dennis Williamson Jan 9 '11 at 15:27

1 Answer 1

up vote 7 down vote accepted

Wrap the variable names in quotes:

mp3file="/media/music/Punk-O-Rama Vol.5 \[MP3PRO\]/01 - Nofx - Pump up the Valium.mp3"
echo "Command: mp3info -x \"$mp3file\""
mp3info -x "$mp3file"

I also removed the superfluous echo commands and escaping that isn't necessary.

share|improve this answer
    
Thanks, it works! Don't know why insisted on preserving escaping that much ;) –  ifischer Jan 9 '11 at 13:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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