I am trying to write a script to automatically add track and title info to an mp3 file's id3 tag.
At this point, I have sed grabbing the info from the filename and formatting it to fit the id3v2 command, but upon hitting a multi-word title, I get the error Couldn't stat file '<second word of title>'
. If I just echo the id3v2 command that is being run, I see the command in this format:
id3v2 -t Title\ Of\ Track -T 1 1_-_Title_Of_Track.mp3
Copy/pasting that text into the terminal executes tagging properly, but of course lacks the intended automation of scripting. So here's what the script intending to automate the process looks like:
#!/bin/bash
for track in *.mp3 ; do
id3v2 $(echo ${track} | sed -e 's/^\([1-9][0-9]*\)_-_\(.*\).mp3/-t \2 -T \1/' -e 's/_/\\ /g') ${track}
done
Any insight as to why the title is treated as a single argument when manually typed but not when scripted?
\
so as to have proper words for the id3 title tag.for f in *.mp3; do id3v2 --TIT3 "${f%.*}" "$f"; done
$()
is done first, then the result is split into words. Addset -x
to the start and you will see output like:id3v2 -t 'Title\' 'Of\' Track -T 1 1_-_Title_Of_Track.mp3
where you can see the backslash serves no purpose.