Search code examples
bashlame

Bash - file path can not be read for LAME encoder


How do I properly escape the path to come out of find to a new command argument?

#!/bin/bash

for f in $(find . -type f -name '*.flac')
       do
           if flac -cd "$f" | lame -bh 320 - "${f%.*}".mp3; then
              rm -f "$f"
              echo "removed $f"
            fi
       done

returns

lame: excess arg Island of the Gods - 3.mp3

Solution

  • Using a Bash for loop is not ideal for the results of find or ls. There are other ways to do it.

    You may want to use -print0 and xargs to avoid word splitting issues.

    $ find [path] -type f -name *.flac -print0 | xargs -0 [command line {xargs puts in fn}]
    

    Or use -exec primary in find:

    $ find [path] -type f -name *.flac -exec [process {find puts in fn}] \;
    

    Alternative, you can use a while loop:

    find [path] -type f -name *.flac | while IFS= read -r fn; do  # fn not quoted here...
      echo "$fn"                                          # QUOTE fn here! 
      # body of your loop
    done