Penguin

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

Quick calculator

    bc <<<"2 + 2"
    4

Note that, while bc(1) supports arbitrary precision, the default number of decimal points it displays is zero:

    bc <<<"3 / 2"
    1

To change this, set the "scale" variable to the number of decimal places you want to see:

    bc <<<"scale = 3; 3 / 2"
    1.500