Executing simple math operations in the terminal (bash, fish)

Published on 2024-07-02 • Modified on 2024-07-02

This snippet shows how to execute simple math operations in the terminal using shells like bash or fish. In this example, we calculate the number of Docker running containers by subtracting one from the number of lines of the docker ps command.
Of course, this is just for the example because we could use the -q option of the docker ps command to remove the header line:
docker ps -q | wc -l
Here is another funny way to get the same result with a local curl command!
curl --unix-socket /var/run/docker.sock http://localhost/containers/json | jq 'length'


# Bash
echo $(( $(docker ps | wc -l) - 1 ))

# Fish
echo (math (docker ps | wc -l) - 1)

 More on Stackoverflow  Random snippet

  Work with me!