Issue
This example is based on swaks, however it's not the only program I use where this problem is doing my head in :)
I have a variable SUBJECT="test subject"
and then I append commands to a string:
SWAKS="
-t $TO
-h-From $FROM
--header Subject:$SUBJECT
--body $BODY
--auth
[email protected]
--auth-password=d83kflb827Ms7d"
Then I'm executing it as ./swaks $SWAKS
but the subject of the message comes as test
as everything after the space is ignored.
I tried changing the quotes into single ones, adding baslashes and nothing works. What am I doing wrong?
My latest try looked like this (output from stderr):
./swaks -t '$TO' '\' -h-From '"test' subject"'
this was using single qoutes at the start and end and double quotes around each variable.
Solution
This is why bash
provides arrays.
SWAKS=(
-t "$TO"
-h-From "$FROM"
--header "Subject:$SUBJECT"
--body "$BODY"
--auth
[email protected]
--auth-password=d83kflb827Ms7d
)
# Appending additional arguments to the array in a loop
for f in attachments/*; do
# Same as SWAKS=( "${SWAKS[@]}" --attach "$f" )
SWAKS+=( --attach "$f" )
done
./swaks "${SWAKS[@]}"
Answered By - chepner