You can use printf
and command substitution:
echo Hello | mailx $(printf -- '-A %s ' output2019*) abc@gamil.com
The --
tells printf that there are no more option arguments (otherwise it will complain about -A
being an invalid option). The '-A %s '
is the format string, every remaining argument (the files matching output2019*.txt
) will be printed using that format. e.g. mailx would be run as:
mailx -A output2019_1.txt -A output2019_2.txt -A output2019_3.txt abc@gamil.com
Note: this won't work with all filenames - it will fail with filenames containing whitespace or shell metacharacters. To cope with those, you can embed quotes in your format string. e.g.
echo Hello | mailx $(printf -- "-A '%s' " output2019*) abc@gamil.com
This will work with filenames that contain any characters except single-quotes.
To work with filenames containing ANY valid character, you'd have to first create an array containing multiple pairs of -A options and filenames. e.g. using process substitution in bash
:
mapfile -d '' -t attachments < \
<(find . -maxdepth 1 -name 'output2019*' -printf '-A\0%p\0')
echo Hello | mailx "${attachments[@]}" abc@gamil.com
When bash expands an array like this (i.e. in double-quotes, with [@]
as the index), each element of the array is treated as a separate "word", and is not subject to any further word splitting or interpretation.
This requires GNU find
(standard on Linux) for the -printf
option. Here we're telling find
to output, for each filename, -A
, a NUL, then the filename followed by another NUL.
The mapfile
command populates an array from stdin, and we're telling it that NUL is the separator character (with -d ''
)...and stdin is redirected (<
) from find ...
via process substitution. See help mapfile
in bash. BTW, readarray
is a synonym for mapfile
in bash.
With the filenames in your example, this would populate the array $attachments
with 6 elements:
$ declare -p attachments
declare -a attachments=([0]="-A" [1]="./output2019_1.txt" [2]="-A" [3]="./output2019_3.txt" [4]="-A" [5]="./output2019_2.txt")
IMO, this is the best version as it will work with any filenames. It's a little more typing, but it's worth it. It's always better to write scripts (and one-liners) defensively, always keeping "what could go wrong?" in mind and making sure that it won't.
-A
, not-a
. Secondlymailx
doesn't support that - you'll need to write a wrapper. – tink Aug 27 '22 at 03:40