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"

A here string uses a form of I/O redirection to feed a command to an interactive program or a command, such as ftp, cat, ex or read. This can be extremely handy. So basically those apps you run that allow user input can be automatically fed strings rather than you typing them!

I find Here Strings very useful to easily assign values to multiple variables.

For Example:

  • 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.

Read from a variable

Make sure the variable is surrounded by double quotes!

  • string="this:is:a:string"; IFS=: ; read foo bar moo cow <<< "$string"
  • echo $foo $bar $moo $cow