## DEV Community is a community of 891,295 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: What are your command line tips?

Abhinav Kulshreshtha

One of my recently acquired command line tip, create this function

? () { echo "\$*" | bc ; }

usage

\$> ? 8*5
40

\$> ? 2+2+9*9+(2-8)
79

No need to create a python process or node process ( or any other interactive shell ) for quick calculation. It just takes the mathematical query and throw it to BC which then return the result.

Abhinav Kulshreshtha • Edited on

In case it isn't obvious, Question mark ? is just a name of function. You can choose anything else as long as it doesn't conflict the system.

Also bc is a mathematical tool, it follows bodmas or pemdas order of operation. So 6 ÷ 2(1+2)= is equal to 1, not 9, not 7, or any other thing you saw on facebook. I should have checked on my machine before bashing out here, My casio calculator outputs 1 because of implied multiplication taking precedence, so does what we are taught in schools in India, but apparently precedence of implied multiplication is standardized to be ignored since 1917.

Lucas Perez • Edited on

Sorry, but 6 ÷ 2(1+2) is just 9, indisputably.

Bodmas, pemdas or any other weird word is just a pitfall to trick people into mistakes, and gladly I have never heard such terms in school. Division and multiplication are performed in the order they appear, it could not be simpler. Division, in fact, is multiplication, just like subtraction is the same as addition. When you divide by 5, you are in fact multiplying by the inverse of 5. There is no reason to perform multiplication before division, since they are the same thing.

I couldn't believe BC would output 1, so I opened a terminal and ran it, and of course it showed 9 in the screen.