Bash script ' single quote in the variable

Hello, please do You have idea how to process file names with ’ in the file name in a bash script?
lets say variable “f” is: abc’d part of filename.ext
I am using “$f” but it still breaks script because the command says

Unrecognized option ’ part of filename.ext’.
Error splitting the argument list: Option not found

i am adding list of files to the while read loop using
done <<< “$files”
and then inside the loop i am using $f variable to print single file:
ffmpeg … -i “$f”
but the single quote in a file name seems to break it.

More detailed code:

files=$(ls -A1 "$d" |grep -Ei ".mp4|.avi);
while read -r f;do
echo "$f"
ffmpeg -nostdin -vaapi_device /dev/dri/renderD128 -i "$f" -vf format=nv12,hwupload -c:v hevc_vaapi -f mkv -rc_mode 1 -qp "$q" "${f%.*}.HEVC.mkv"
# -v debug
done <<< "$files"

Question is how to add list of files to the loop so if file contains single quote character in the name, it will not “break” ffmpeg command.

This is bad and you shouldn’t parse the output of ls command. Instead try something as follows:

for f in $d/*.mp4
do
    echo "Working on $f"
    ffmpeg -nostdin -vaapi_device /dev/dri/renderD128 -i "$f" -vf format=nv12,hwupload -c:v hevc_vaapi -f mkv -rc_mode 1 -qp "$q" "${f%.*}.HEVC.mkv"
done
1 Like

For multiple extensions make a function and call it

# set q here 
q=""
helper(){
    local p="$*"
    for f in $p
    do
        echo "Working on $f"
        ffmpeg -nostdin -vaapi_device /dev/dri/renderD128 -i "$f" -vf format=nv12,hwupload -c:v hevc_vaapi -f mkv -rc_mode 1 -qp "$q" "${f%.*}.HEVC.mkv"
    done
}

helper '/tmp/r/*.mp4'
helper '/tmp/r/*.avi'
helper '/path/to/*.mkv'
1 Like

for f in $d/*.mp4

Thank you for help, it seems to be working. ::
I have only added quotes: for f in “$d”/*.mp4

Also I suggest analysis all your script using a tool named shellcheck which is is a static analysis and linting tool for sh/bash scripts:

1 Like

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki