Penguin
Note: You are viewing an old revision of this page. View the current version.

Here Strings

A variant of here documents, the format is:

  • command <<< "string"

The string is supplied to the command on its standard input. Remember of course, that "string" can be replaced with a variable.

I find Here String's very useful to easily assign values to multiple variables.

The 'read' command claims to read from the standard input, but it doesn't actually seem to happen when a pipe is involved. Each command in the pipeline gets executed in its own subshell and so the variables are set there and not passed back to the parent process. This is why

  • echo "red blue red" | read a b c

DOES NOT WORK

How to get around this problem using Here Strings:

  • IFS=: ; read foo bar moo <<< "yellow:red:blue";

Now try:

  • echo $foo $bar $moo

and hopefully the value of the three variables will be displayed for you.