# Floating-Point Math and the bc Utility

Have you ever needed to do some floating-point math in a shell script? If the answer is yes, then you' in luck. On UNIX machines there is an interactive program called bc that is an interpreter for the arbitrary-precision arithmetic language. You can start an interactive session by typing bc on the command line. Once in the session you can enter most complex arithmetic expressions as you would in a calculator. The bc utility can handle more than I can cover in this chapter, so we are going to keep the scope limited to simple floating-point math in shell scripts.

In this chapter we are going to create shell scripts that add, subtract, multiply, divide, and average a list of numbers. With each of these shell scripts the user has the option of specifying a scale, which is the number of significant digits to the right of the decimal point. If no scale is specified, an integer value is given in the result. Because the bc utility is an interactive program, we are going to use a here document to supply input to the interactive bc program. We cover using a here document in detail throughout this chapter. Of course, we can write an expect script to handle the automation. Sounds like a good Lab Assignment.

# Syntax

By now you know the routine: We need to know the syntax before we can create a shell script. Depending on what we are doing we need to create a mathematical statement to present to bc for a here document to work. A here document works ...

Get Mastering UNIX® Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8806097,"math_prob":0.80292434,"size":1804,"snap":"2020-24-2020-29","text_gpt3_token_len":376,"char_repetition_ratio":0.10611111,"word_repetition_ratio":0.0,"special_character_ratio":0.20177384,"punctuation_ratio":0.11235955,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9506191,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-14T04:52:15Z\",\"WARC-Record-ID\":\"<urn:uuid:64893634-0b01-44a8-a501-0c704f768aee>\",\"Content-Length\":\"36914\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:73a232e7-1f7c-4c8a-b0c2-a5d6c5110a00>\",\"WARC-Concurrent-To\":\"<urn:uuid:b7ed3a8d-cfbb-40f5-9c98-719cc6d37d67>\",\"WARC-IP-Address\":\"104.117.6.234\",\"WARC-Target-URI\":\"https://www.oreilly.com/library/view/mastering-unix-shell/9780470183014/24_chapter13.html\",\"WARC-Payload-Digest\":\"sha1:I6L5HQHCTLUQ7OBHNW2OJN32OYJGM2TT\",\"WARC-Block-Digest\":\"sha1:LZQZ4TGCURQU7I37BJVJMJEU75FAW5NE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593657147917.99_warc_CC-MAIN-20200714020904-20200714050904-00084.warc.gz\"}"}