1

This shell command converts all .avi to .mp4 as per this answer by @llogan

Can someone explain how ${i%.*} works? In particular, what does % do?

for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done
wongz
  • 1,184
  • 2
  • 9
  • 19

1 Answers1

3

From BashGuide/Parameters/Parameter Expansion:

${parameter%pattern}
The pattern is matched against the end of parameter. The result is the expanded value of parameter with the shortest match deleted.

From How do I do string manipulations in bash?:

The % means "remove the shortest possible match from the end of the variable's contents"

In other words, if the expanded value of parameter (i in your case) is myvideo.avi, then %.* will result in myvideo. This is so the output file does not end up being named myvideo.avi.mp4, and instead is named myvideo.mp4.

llogan
  • 105,293
  • 25
  • 200
  • 219