How to iterate over multiline variable in bash?

Viewed 2184

Given

# this is programmatically generated... hard-coded here for this example
commands="\nthing1@this is thing 1!\nthing2@this is thing 2!"

while read -r line; do
  cmd=$(printf "$line" | cut -d'@' -f1 | tr -d '\n')
  desc=$(printf "$line" | cut -d'@' -f2 | tr -d '\n')

  printf '  %-20s %s\n' "$cmd" "$desc"
done <<< "$commands"

I'd expect that to output

  thing1        this is thing1!
  thing2        this is thing2!

Instead I get

  thing1thing2        this is thing1!this is thing2!

What am I doing wrong?

I want to read over the newlines, so I can then cut on @ and printf accordingly.

2 Answers
Related