0

I am trying to place a line of a text file, directly in a mail function. The string has the ' ' around it in the text file.

mailx -s "New Member" "cat ../address" 

A line in ../address is 'my-email@gmail.com' including the ' ' quotes.

The cat ../address is not the best way of doing it but I do not know any other way to try.

Required result should be:

mailx -s "New Member" 'my-email@gmail.com'
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
user3386373
  • 129
  • 1
  • 1
  • 10

2 Answers2

1

You need to remove the literal ' chars. from the line in order for the address to be passed correctly to mailx:

There's more than one way to do it:

mailx -s "New Member" "$(tr -d "'" < ../address)"

mailx -s "New Member" "$(xargs < ../address)"

Note: The above assumes that ../address contains only 1 line.

@chepner makes an important point:

storing the quotes in the file and then reading them in [and using the result unquoted] is not the same as quoting the command substitution.

To elaborate on this further:

  • The OP shows 'my-email@gmail.com' as the desired argument to pass to mailx. In this direct form of single-quoting, bash will remove the quotes (see Quote Removal section in man bash) before handing the - unexpanded - string contents, without the quotes, to mailx.

  • Indirect quoting does NOT work: Reading in a string that happens to contain literal quote characters around it is NOT subject to quote removal by bash, so the enclosing quotes are passed through as part of the string - mailx would see an invalid email address that starts (and ends) with a '.

  • Therefore, the solution is:

    • remove the quotes from the input string first
    • then use direct quoting to protect the result from (further) shell expansion; note that double-quoting is needed so as to make sure the command substitution (($...)) is evaluated.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Deleting my answer as redundant, but want to emphasize for the OP that storing the quotes in the file and then reading them in is not the same as quoting the command substitution. – chepner Apr 17 '14 at 17:35
  • @chepner: I was about to up-vote yours for making that very point :) I'll incorporate it into the answer. – mklement0 Apr 17 '14 at 17:38
0

Use backticks for command substitution. I assume there is only one line in the file.

mailx -s "New Member" `cat ../address`

An alternative/better form for backticks is $()

mailx -s "New Member" $(cat ../address)
SzG
  • 12,333
  • 4
  • 28
  • 41
  • 2
    Don't use backticks: use `$(...)`, which is both more readable and nestable. – chepner Apr 17 '14 at 17:28
  • @chepner, is `$()` compatible with the Bourne shell, or is it a bashism? – SzG Apr 17 '14 at 17:29
  • 1
    @SzG: It's part of the POSIX standard - see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03 – mklement0 Apr 17 '14 at 17:30
  • Also, note that POSIX != Bourne shell. The actual Bourne shell is relatively limited compared to the standard, but the two are often confused because the standard specifies the shell be available as `/bin/sh`, which was the traditional name of the Bourne shell. – chepner Apr 17 '14 at 17:34
  • 1
    @chepner: Excellent point, thanks for making that clear. @SzG: Note that the input file contains literal `'` chars, which must be stripped first - see @chepner's comments. – mklement0 Apr 17 '14 at 17:36
  • The OP stated he wants the quotes. – SzG Apr 17 '14 at 17:41
  • @chepner: Which one is most portable, the backticks, or `$()`? – SzG Apr 17 '14 at 17:41
  • Thank you for answering the question. Also the additional comments of others. – user3386373 Apr 17 '14 at 17:54
  • Both are equally portable. – chepner Apr 17 '14 at 18:02
  • @SzG: Re quoting: please see the explanation I added to my answer. – mklement0 Apr 17 '14 at 18:08