Bash: Math and Variables

Example: Math and Variables in Bash


	#!/bin/bash
	# A sample Bash script by John Smith
	firstname=John
	lastname=Smith
	address='100 Main Street'
	echo $firstname $lastname
	echo $address
	

Output:

  John Smith
  100 Main Street


Example: Math and Variables in Bash


	#!/bin/bash
	# A sample Bash script by John Smith
	echo "3*2" | bc

	declare -i total   # needed for next line to work; declare n integer
	total=5-3
	echo $total

	x=10
	y=2
	echo $x+$y | bc
	awk "BEGIN {printf \"%.2f\n\", 100/3}"
	awk "BEGIN {printf \"%.2f\n\", $x}"
	

Output:

  6
  2
  12
  33.33
  10.00


Example: Math and Variables in Bash


	#!/bin/bash
	# A sample Bash script by John Smith
	let f=5
	echo $f

	let g=$f+1
	echo $g

	((h=2))
	echo $h

	((h++))
	echo $h
	

Output:

  5
  6
  2
  3