I'm trying to create a bash script to run the ffmpeg command on every file in a directory with filenames that contain spaces. This is the script that I started with to do an individual file which works properly. I use tab
to populate the filename argument which includes an escape character for all the spaces:
#!/bin/bash
input="./"$1
output="/xfer/transfer/temp/"$1
ffmpeg -i "${input}" -c:v copy -c:a libfdk_aac -b:a 160k "${output}"
Next step is to perform the ffmpeg command on all files in a directory. Let's say the directory looks like this with the ls
command:
'Name of First File.mkv'
'Name of Second File.mkv'
Here's my test code:
#!/bin/bash
files=$(ls *)
for input in ${files}
do
echo $input
done
My problem is the output looks like this:
Name
of
First
File.mkv
Name
Of
Second
File.mkv
Adding a -b
to the ls command simply adds a backslash to the end of every line. I need help with making $input
look like Name of First File.mkv
, etc.
ls
output for anything.ls
is a tool for interactively looking at directory metadata. Any attempts at parsingls
output with code are broken. Globs are much more simple AND correct:for file in *.txt
. Read http://mywiki.wooledge.org/ParsingLs – Gilles Quénot Sep 30 '23 at 19:18http://mywiki.wooledge.org/Quotes
– Gilles Quénot Sep 30 '23 at 19:20http://mywiki.wooledge.org/Arguments
https://web.archive.org/web/20230224010517/https://wiki.bash-hackers.org/syntax/words
when-is-double-quoting-necessary