New answers tagged

5

bash does use 64bit integers: $echo $((2**63-1)) 9223372036854775807 $echo $((2**63)) -9223372036854775808


9

Given that printf 'eth0 Download rate: %s B/s\n' "$((eth0_rx2-eth0_rx1))" is giving you the correct value, as long as integer arithmetic is good enough, you’ve got your answer: $((eth0_rx2-eth0_rx1)), i.e. shell arithmetic. Many shells, notably Bash, use 64-bit integers, even on 32-bit platforms. Thus: eth0_diff=$((eth0_rx2 - eth0_rx1)) ... ...


4

$(cmd) gets the standard output of cmd¹, so for that to expand to the result, you need cmd to output it: some_func() { echo "$(( 3 + 5 ))" } [ "$(some_func)" -lt 10 ] as others have already said. However, that means some_func is run in a subshell environment, so any modification to variables or anything else will be lost afterwards. ...


6

It should be some_func() { echo "$(( 3 + 5 ))" } [ "$(some_func)" -lt 10 ] Why it failed $(some_func) expands to the output of the function* (minus its trailing newline), which however outputs nothing. Therefore, the test becomes [ -lt 10 ] In its most basic forms, the [ test accepts 1 to 3 parameters. Since above there are 2 ...


1

Command substitution captures the output of a command or function. That function has no output. The $? variable holds the return code of the function. Either do this: some_func (( $? < 10 )) && echo yes or change the function to: some_func() { echo $(( 3 + 5 )) } [[ $(some_func) -lt 10 ]] && echo yes Notice how I'm using [[...]] ...


Top 50 recent answers are included