0

I am trying to build shell command in with bahs loop from a file.

I have file with contents like this:

artist="artist"
title="title"
composer=""

and loop like this:

grep -v '=""' "$dir/input.file" | while read line || [[ -n $line ]];
do
    tag+="-metadata ${line} ";
    echo $tag;
done    
echo "ffmpeg -n -i "$dir/$file" -loglevel error -map_metadata 0 $tag";

while in a loop it works fine:

-metadata artist="artist"
-metadata artist="artist" -metadata title="title"

however final output does not contain what was build in loop

ffmpeg -n -i /somepath -loglevel error -map_metadata 0 

what am i doing wrong?

daba
  • 155
  • 1
  • 6
  • 2
    Does this answer your question? [Why variable values are lost after terminating the loop in bash?](https://stackoverflow.com/questions/26144029/why-variable-values-are-lost-after-terminating-the-loop-in-bash). Short solution: Use `shopt -s lastpipe; grep ... | while ...` or `while read ... done < <(grep ...)`. – Socowi Apr 09 '21 at 17:32
  • See also [BashFAQ/024](https://mywiki.wooledge.org/BashFAQ/024). – Benjamin W. Apr 09 '21 at 17:56
  • thanks redid my loop like this: while read line; do tag+="-metadata ${line} "; echo $tag; done << $(grep -v '=""' "$dir/$file") echo "ffmpeg -n -i "$dir/$file" -loglevel error -map_metadata 0 $tag"; and now i get this result: -metadata -metadata -metadata echo "ffmpeg -n -i "/somepath/" -loglevel error -map_metadata 0"; – daba Apr 11 '21 at 09:20

0 Answers0