🤖
Guides
  • Introduction
  • Beginners
    • Getting Started
  • Guides
    • SQLi Walkthrough
    • My First BoF
    • OSCP Buffer Overflow Guide (Windows)
    • Parrot OS customisation
    • Terminal Customisation
    • Video Guides
  • Cheat Sheets
    • Reverse Shells
    • Tunnelling, Pivoting and Proxies
    • SQL Injection
      • WAF Bypass
      • SQLMap
      • DBMS Cheatsheets
        • MSSQL
        • MySQL
        • Oracle
        • SQLite
        • PostgreSQL
      • References
    • Bash Cheat Sheets
      • Terminal
      • Find
      • Grep
      • Sed
      • Awk
      • Xargs
      • System
      • Download
      • Networking
      • Hardware
      • Variable
      • Math
      • Data Manipulation
      • Random
      • Time
      • Condition and Loop
      • Other
    • OSINT
    • Ping Sweeps
  • Methodologies
    • VOIP Checklist
    • OWASP v4 Checklist
    • External Inf
    • Internal Infrastructure
  • Linux
    • Checklist - Linux Priv Esc
  • Windows
    • Checklist - Windows Priv Esc
  • Things to do/look at
Powered by GitBook
On this page
  • Math
  • Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)
  • Print out the prime factors of a number (e.g. 50)
  • Sum up input list (e.g. seq 10)
  • Sum up a file (each line in file contains only one number)
  • Column subtraction
  • Simple math with expr
  • More math with bc

Was this helpful?

  1. Cheat Sheets
  2. Bash Cheat Sheets

Math

Math

Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)

echo $(( 10 + 5 ))  #15
x=1
echo $(( x++ )) #1 , notice that it is still 1, since it's post-incremen
echo $(( x++ )) #2
echo $(( ++x )) #4 , notice that it is not 3 since it's pre-incremen
echo $(( x-- )) #4
echo $(( x-- )) #3
echo $(( --x )) #1
x=2
y=3
echo $(( x ** y )) #8

Print out the prime factors of a number (e.g. 50)

factor 50
# 50: 2 5 5

Sum up input list (e.g. seq 10)

seq 10|paste -sd+|bc

Sum up a file (each line in file contains only one number)

awk '{s+=$1} END {print s}' filename

Column subtraction

cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'

Simple math with expr

expr 10+20 #30
expr 10\*20 #600
expr 30 \> 20 #1 (true)

More math with bc

# Number of decimal digit/ significant figure
echo "scale=2;2/3" | bc
#.66

# Exponent operator
echo "10^2" | bc
#100

# Using variables
echo "var=5;--var"| bc
#4
PreviousVariableNextData Manipulation

Last updated 4 years ago

Was this helpful?