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
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉
