< Previous | Contents | Next >
Using bc
If we save the bc script above as foo.bc, we can run it this way:
[me@linuxbox ~]$ bc foo.bc
bc 1.06.94
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'.
4
[me@linuxbox ~]$ bc foo.bc
bc 1.06.94
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'.
4
If we look carefully, we can see the result at the very bottom, after the copyright message. This message can be suppressed with the -q (quiet) option.
bc can also be used interactively:
[me@linuxbox ~]$ bc -q 2 + 2
4
quit
[me@linuxbox ~]$ bc -q 2 + 2
4
quit
When using bc interactively, we simply type the calculations we wish to perform, and the results are immediately displayed. The bc command quit ends the interactive ses- sion.
It is also possible to pass a script to bc via standard input:
[me@linuxbox ~]$ bc < foo.bc
4
[me@linuxbox ~]$ bc < foo.bc
4
The ability to take standard input means that we can use here documents, here strings, and pipes to pass scripts. This is a here string example:
[me@linuxbox ~]$ bc <<< "2+2"
4
[me@linuxbox ~]$ bc <<< "2+2"
4