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?
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 setIFS
without usingecho
: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.