Bash shortcuts

In this section, you'll see how to execute Python instructions from the command line and use shell shortcuts to create simple CLI applications. This project uses bash as the shell to showcase examples.

Python CLI options

Passing a file to the interpreter from the command line is one of the ways to execute a Python program. You can also use -c option to directly pass instructions to be executed as an argument. This is suitable for small programs, like getting the result of a mathematical expression. Here's an example:

# use py instead of python3.9 for Windows
$ python3.9 -c 'print(5 ** 2)'
25

info Use python3.9 -h to see all the available options. See docs.python: Command line and environment for documentation.

Python REPL

If you call the interpreter without passing instructions to be executed, you'll get an interactive console known as REPL (stands for Read Evaluate Print Loop). This is typically used to execute instructions for learning and debugging purposes. REPL is well suited to act as a calculator too. Since the result of an expression is automatically printed, you don't need to explicitly call print() function. A special variable _ holds the result of the last executed expression. Here's some examples:

$ python3.9 -q
>>> 2 * 31 - 3
59
>>> _ * 2
118
>>> exit()

See also:

Bash function

Calling print() function via -c option from the command line is simple enough. But you could further simplify by creating a CLI application using a bash function as shown below.

# bash_func.sh
pc() { python3.9 -c 'print('"$1"')' ; }

You can type that on your current active terminal or add it your .bashrc file so that the shortcut is always available for use (assuming pc isn't an existing command). The function is named pc (short for Python Calculator). The first argument passed to pc in turn is passed along as the argument for Python's print() function. To see how bash processes this user defined function, you can use set -x as shown below. See unix.stackexchange: How to debug a bash script? for more details.

$ set -x
$ pc '40 + 2'
+ pc '40 + 2'
+ python3.9 -c 'print(40 + 2)'
42

# don't forget to quote your argument, otherwise spaces
# and other shell metacharacters will cause issues
$ pc 40 + 2
+ pc 40 + 2
+ python3.9 -c 'print(40)'
40

$ set +x
+ set +x

Here's some more examples of using pc as a handy calculator from the command line.

$ pc '2 * 31 - 3'
59

$ pc '0xfe'
254

$ pc '76 / 13'
5.846153846153846
$ pc '76 // 13'
5

info See also unix.stackexchange: when to use alias, functions and scripts

Accepting stdin

Many CLI applications allow you to pass stdin data as input. To add that functionality, you can use if statement to read a line from standard input if the number of arguments is zero or - character is passed as the argument. The modified pc function is shown below:

# bash_func_stdin.sh
pc()
{ 
    ip_expr="$1"
    if [[ $# -eq 0 || $1 = '-' ]]; then
        read -r ip_expr
    fi
    python3.9 -c 'print('"$ip_expr"')'
}

Here's some examples. Use set -x if you wish to see how the function gets evaluated for these examples.

$ source bash_func_stdin.sh

$ echo '97 + 232' | pc
329

$ echo '97 + 232' | pc -
329

$ pc '32 ** 12'
1152921504606846976

info See wooledge: Bash Guide and ryanstutorial: Bash scripting tutorial if you'd like to learn more about bash shell scripting. See also shellcheck, a linting tool to avoid common mistakes and improve your script.