0

I'm grabbing the tail of my output file and then want to add 0.000001 to that value.

So far I've tried it this way:

offset=0.000001

time=$(($(`tail -1 speed.txt1`) + $offset))
marti
  • 69
  • 7
  • please update the question with sample inputs and the desired output; does `tail -1 speed.txt1` return an integer, float, e-notation number, something else? – markp-fuso Nov 30 '21 at 19:56

1 Answers1

3

Bash itself does not have any floating point ability. You need to use another tool to get a float result in the shell.

Given:

cat file
1
2
3

You can use bc for floating point in the shell:

offset='0.000001'
printf "%s+%s\n" $(tail -1 file) "$offset" | bc
3.000001

Or awk:

awk -v offset="$offset" 'END{printf "%.7f\n", $1+offset}' file
3.000001

Since bc does not understand scientific notation (ie, 1.3E6) and you can still normalize that with printf in the shell:

x=1.33E6
printf "%f\n" "$x"
1330000.000000

And then pass to bc:

offset="1e-6"
x=1.33E6
printf "%f+%f\n" "$x" "$offset"
1330000.000000+0.000001

printf "%f+%f\n" "$x" "$offset" | bc
1330000.000001
dawg
  • 98,345
  • 23
  • 131
  • 206