2
#!/bin/bash

for a in ./*.flac; do
  ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}"
done

I found this script a few days ago to convert all FLAC files in the current directory into the MP3 format.

What I don't understand here is the "${a[@]/%flac/mp3}" part. I think it replaces the ending flac with mp3 for the current filename. But what eactly does the [@] part do? Is it a regular expression?

monkey2k
  • 23
  • 3

2 Answers2

4

As you guessed it, ${var/%flac/mp3} replaces the ending "flac" (if any) in value $var with "mp3". "${var[@]/%flac/mp3}" would do the same on each element output by ${var[@]} if var was an array.

Here, since a is not an array, you can remove [@] (I assume this is an heritage from previous attempts by the programmer).

xhienne
  • 17,075
  • 2
  • 52
  • 68
2

This does a simple shell expansion. The variable a will iterate through every file matching the *.flac glob.

Since each entry will be a file (and I'm presuming no spaces in the filenames), foo.flac will be the expansion of both $a and ${a[@]}.

The construct ${var/%foo/bar} will replace foo with bar at the end of a variable var. So it's replacing the extension flac with the extension mp3 in your example to provide ffmpeg with the output filename.

DopeGhoti
  • 73,792
  • 8
  • 97
  • 133