Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's no good way of doing this without an array. (There's a not good way of doing it, using <code>eval</code>, but the array is simpler.)</p> <p>The problem is that you want each file to end up as one argument to <code>email</code>, but you want to accumulate all those arguments into a bash variable. There's just no way to do that: you can cause the bash variable to be inserted as a single argument (<code>"$arg"</code>) or you can cause it to be inserted as however many words it gets split into (<code>$arg</code>), but you can't get it to be split according to whether or not spaces were escaped when the variable was created, because the bash only remembers the string assigned to a variable, not the escape marks.</p> <p>However, you can do it with an array, because you can make every filename exactly one array element, and you can get bash to insert an array as one argument per element.</p> <p>Here's how:</p> <pre><code># file paths destinationPath="/cygdrive/c/Documents and Settings/Eric/My Documents/" attachments="\n2013-12-12.pdf" body="Test Body" recipient="asdf@asdf.com" # prepare attachments args=() for file in $attachments ; do file=${file//[ \\n]/} touch $file mv $file "$destinationPath/$file" args+=(-a "$destinationPath/$file") done # send email echo -e $body | email --from-addr nosender@mydomain.com \ --from-name "Automated CSS Downloader" \ --subject "Downloaded new file(s) from CSS" \ "${args[@]}" eric@mydomain.com </code></pre> <p>You might also want to make the <code>attachments</code> variable into an array; from the presence of the newline character, I'm assuming that you're actually setting it in some more complicated way, so I didn't change the code. But your current code won't work if the attachment name has a space in it (and I'm pretty sure that the newline will be eliminated by the parameter expansion in the <code>for</code> form, unless you've altered the value of <code>$IFS</code>, so <code>file=${file//[ \\n]/}</code> shouldn't be necessary.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload