utility

Bash awk fractional calculation

External awk performs floating-point calculations in ordinary implementations. Pass validated numeric data as separate arguments; formatting does not guarantee exact decimal arithmetic.

Formal Syntax & Structure

bash syntaxbash
awk -v name=value 'BEGIN { printf format, expression }'

Return Value

formatted output

External awk performs floating-point calculations in ordinary implementations. Pass validated numeric data as separate arguments; formatting does not guarantee exact decimal arithmetic.

Working Example

Example Snippetbash
part=3total=8LC_ALL=C awk -v part="$part" -v total="$total" 'BEGIN { printf "%.2f%%\n", 100 * part / total }'
Bash Reference