Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to deal with files that have spaces in their names when passing it to a command

I’m new to bash and writing a shell script to add some metadata to music files and there are some spaces in filename

FFMPEG_TARGET_COMMAND="\"$INPUT_DIRECTORY\" $SOME_METADATA_ARG \"$OUTPUT_DIRECTORY\""

echo $FFMPEG_TARGET_COMMAND
> "/user/musicEx/Dream In Drive.mp3" \
-metadata album="soundtrack" \
-codec copy "/user/output/Dream In Drive.mp3"
ffmpeg -i $FFMPEG_TARGET_COMMAND

and I got "/user/musicEx/Dream: No such file or directory

But if I execute in shell by typing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ffmpeg -i "/user/musicEx/Dream In Drive.mp3" \
-metadata album="soundtrack" \
-codec copy "/user/output/Dream In Drive.mp3"

this will work

I wonder why. I’ve already escape the quote in $FFMPEG_TARGET_COMMAND. I tried to escape space character, but it didn’t work too.

also, I tried to put all of them into one variable:

COMMAND="ffmpeg -i \"/user/musicEx/SCARLET NEXUS.mp3\"  -codec copy \"/user/output/SCARLET NEXUS.mp3\""

and I execute it by $COMMAND

So, in script, how to deal with files that have spaces in their names?

>Solution :

Use bash arrays.

SOME_METADATA_ARGS=( -metadata album="soundtrack" )
ffmpeg_args=( "$INPUT_DIRECTORY" "${SOME_METADATA_ARGS[@]}" -codec copy "$OUTPUT_DIRECTORY" )
ffmpeg -i "${ffmpeg_args[@]}"

how to deal with files that have spaces in their names when passing it to a command

Properly quote variable expansions to prohibit word splitting expansion. Check your scripts with shellcheck. It’s not what you put in the variable, it’s how you use the variable. Read https://mywiki.wooledge.org/BashFAQ/020 and
https://mywiki.wooledge.org/BashFAQ/050 .

No reason to SHOUT_THAT_MUCH. Prefer to use lower case variables for local non-exported variables.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading