# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alomancy.gitbook.io/guides/cheat-sheets/bash-cheat-sheets/math.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
