0

I have a list of files (to be e-mailed). I need to append "-a" to each element of the list (this is a mutt requirement).

I tried this:

attachment+=(../*.log) 
attachments+=(-a "${attachment[@]}" )

But echo shows -a is append only one time (at the beginning). How do a I get a result like the following?

-a file1.log -a file2.log -a file3.log ...

This question appeared here.

Community
  • 1
  • 1
KcFnMi
  • 5,516
  • 10
  • 62
  • 136

2 Answers2

0

You can use mapfile command available in BASH 4 with printf:

mapfile -t attachments < <(printf -- '-a %s\n' "${attachment[@]}")

printf will add -a before each array element in attachment array.

This is assuming there are no newlines in your filenames.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Try this:

mutt $(ls ../*.log|while read f;do echo -a \"$f\";done) ...

To explain, per jpaugh's comment:

When ls is used in a pipeline | (rather than alone), it puts one file per line (rather than making a table).

while read <var>;do ... done is a standard shell pattern for reading single lines from the input (into var).

Putting " around the filename $f protects against files/paths with spaces in the name.

Finally, the $(...) (replacement for the old deprecated backtick ` operator) puts all command output back into a flat list (single line).

Jeff Y
  • 2,437
  • 1
  • 11
  • 18