Penguin
Annotated edit history of HereStrings version 10, including all changes. View license author blame.
Rev Author # Line
4 BenStaz 1 !Here Strings
7 BenStaz 2 A variant of [Here Documents|HereDocuments] the format is:
4 BenStaz 3
4 *command <<< "string"
5
5 BenStaz 6 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!
4 BenStaz 7
6 GreigMcGill 8 I find Here Strings very useful to easily assign values to multiple variables.
4 BenStaz 9
10 For Example:
11 *IFS=: ; read foo bar moo <<< "yellow:red:blue";
12
13 Now try:
14
15 *echo $foo $bar $moo
16
17 and hopefully the value of the three variables will be displayed for you.
8 BenStaz 18
19 !Read from a variable
9 BenStaz 20 Make sure the variable is surrounded by *double* quotes!
8 BenStaz 21
22 *string="this:is:a:string"; IFS=: ; read foo bar moo cow <<< "$string"
23 *echo $foo $bar $moo $cow
10 LawrenceDoliveiro 24
25 !Quick calculator
26 <pre>
27 bc <<<"2 + 2"
28 4
29 </pre>
30 Note that, while bc(1) supports arbitrary precision, the default number of decimal points it displays is zero:
31 <pre>
32 bc <<<"3 / 2"
33 1
34 </pre>
35 To change this, set the "scale" variable to the number of decimal places you want to see:
36 <pre>
37 bc <<<"scale = 3; 3 / 2"
38 1.500
39 </pre>