InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Creating a Command-Line Calculator in bash
Bash

Creating a Command-Line Calculator in bash

InfinityCoder February 19, 2017

You need more than just integer arithmetic, and you’ve never been very fond of RPN notation.

How about a different approach to a command-line calculator?

Create a trivial command-line calculator using awk’s built-in floating-point arithmetic expressions:

1
2
3
4
5
6
7
# cookbook filename: func_calc
 
# Trivial command line calculator
function calc
{
   awk "BEGIN {print \"The answer is: \" $* }";
}

You may be tempted to try echo The answer is:$(( $* )), which will work fine for integers, but will truncate the results of floating-point operations.
We use a function because aliases do not allow the use of arguments.
You will probably want to add this function to your global /etc/bashrc or local ~/.bashrc.
The operators are what you’d expect and are the same as in C:

1
2
3
4
5
$ calc 2 + 3 + 4
The answer is: 9
 
$ calc 2 + 3 + 4.5
The answer is: 9.5

Watch out for shell meta characters. For example:

1
2
$ calc (2+2-3)*4
-bash: syntax error near unexpected token `2+2-3'

You need to escape the special meaning of the parentheses.

You can put the expression inside single quotes, or just use the backslash in front of any special (to the shell) character to escape its meaning.

For example:

1
2
3
4
5
6
7
8
$ calc '(2+2-3)*4'
The answer is: 4
 
$ calc \(2+2-3\)\*4
The answer is: 4
 
$ calc '(2+2-3)*4.5'
The answer is: 4.5

We need to escape the multiplication symbol too, since that has special meaning to bash as the wildcard for filenames.

This is especially true if you like to put whitespace around your operators, as in 17 + 3 * 21, because then * will match all the files in the current directory, putting their names on the command line in place of the asterisk—definitely not what you want.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Parsing Output into an Array in bash
You want the output of some program or script to …

Parsing Output into an Array in bash

Deciding Whether a Command Succeeds in bash
You need to run some commands, but you only want …

Deciding Whether a Command Succeeds in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap