7

I can use herestrings to pass a string to a command, e.g.

cat <<< "This is a string"

How can I use herestrings to pass two strings to a command? How can I do something like

### not working
diff <<< "string1" "string2"

### working but overkill
echo "string1" > file1
echo "string2" > file2
diff file1 file2
8
  • This is a here string, not a here document. Commented Nov 11, 2013 at 10:55
  • Are you trying to pass two strings to diff? Commented Nov 11, 2013 at 10:57
  • 2
    diff <( echo "string1" ) <( echo "string2" ) will work. Commented Nov 11, 2013 at 11:01
  • 1
    @pfnuesel Why diff strings? Why not [ "$string1" = "$string2" ] && echo equal || echo "not equal"? Commented Nov 11, 2013 at 11:03
  • 2
    In case you're wondering, your diff <<< "string 1" "string 2" is parsed as a call to diff with a single command-line argument "string 2" and standard input bound to a "string 1"-seeding stream. It expects two command-line arguments, sees only one, and stops there, not consuming the standard input. You could actually get it to work by comparing to standard input: diff - file2 <<< "string 1" Commented Nov 11, 2013 at 11:15

2 Answers 2

10

You can't use two herestrings as input to the same command. In effect, the latest one will replace all others. Demonstration:

cat <<< "string 1" <<< "string 2" <<< "string 3"
# only shows "string 3"

On the other hand, if what you want is really diff two immediate inputs, you can do it this way:

diff <(echo "string 1") <(echo "string 2")
Sign up to request clarification or add additional context in comments.

4 Comments

That works fine. Can you explain me why I can't have a space between < and (?
@pfnuesel Please refer to the Process Substitution documentation.
About the no space between < and (, follow @gniourf_gniourf's link (+1). It could be said it's just defined this way, like you can't put a space in the middle of a [[ or of a <<<. There's probably an actual ambiguity-resolving reason behind as well.
Also note that there are different ways to “pass strings to commands” involved in your question. Herestrings, redirections, heredocs work by providing the data on standard input, to be read as a stream; the echo construct passes strings as command-line parameters; process substitution (the <() construct) is a bit more involved but ends up providing a file name as a command-line parameter. All those construct fit the definition of “passing strings to commands”, so your question was pretty ambiguous. (comment rewritten because of the huge mistake that haunted its previous version)
2

You can simply concatenate the two strings:

cat <<< "string1""string2"

(not the lack of space between the two). The here string now consists of a single word whose contents are the contents of the two strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.