Question

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'
Was it helpful?

Solution

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.

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top