Sample Header Ad - 728x90

How can I feed a string to awk for the purpose of mathematical calculation?

2 votes
4 answers
1943 views
#!/bin/sh -- expression_to_evaluate='12 + 3' printf '%s\n' "Arithemtic Expansion: $(( $expression_to_evaluate ))" printf '%s' 'bc: ' printf '%s\n' "$expression_to_evaluate" | bc printf '%s' 'awk: ' awk -v expression_to_evaluate="$expression_to_evaluate" -- 'BEGIN{printf "%d\n", expression_to_evaluate}' Output: Arithemtic Expansion: 15 bc: 15 awk: 12 awk is returning 12 instead of 15. How can I feed a string to awk for the purpose of mathematical calculation?
Asked by Harold Fischer (1974 rep)
Mar 1, 2019, 05:44 AM
Last activity: May 20, 2022, 07:33 PM