Search code examples
bashmutt

Add flag to each item in list


I wanted to attach a list of png's to an email with mutt so I did this

attachment=""
for png in $(ls *.png); do attachment="$attachment -a $png"; done
mutt $attachment 

To get this command mutt -a pic1.png -a pic2.png -a pic3.png. Any better ideas on how to do this?


Solution

  • Since mutt will accept the command as:

    mutt -apic1.png -apic2.png -apic3.png
    

    (with no space between -a and the filename), a wonderful possibility is the following:

    shopt -s nullglob
    
    files=( *.png )
    mutt "${files[@]/#/-a}"
    

    The shopt -s nullglob so that a non-matching glob expands to nothing.

    The expansion of "${files[@]/#/-a}" will be like the expansion of "${files[@]}", but with -a prepended to each field.

    This is 100% safe if you have filenames with spaces or other funny symbols.