Is $(( expr )) equivalent to $[ expr ] in bash? [duplicate] - bash

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
bash: $[<arithmetic-expression>] vs. $((<arithmetic-expression>))
The $(( expr )) construct can be used for integer math in bash, e.g.
echo $(( 2*2 + 1 )) # 5
$[ expr ] seems to do to do the same (but isn't documented):
echo $[ 2*2 + 1 ] # 5
Are these constructs equivalent in bash?

man bash says:
The format for arithmetic expansion is:
$((expression))
The old format $[expression] is deprecated and will be removed in upcoming versions of bash.

Related

Why is bash behaving differently when it takes argument in braces? [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 1 year ago.
Input: range 6
function range {
echo {0..$1}
echo {0..6}
if [[ $1 =~ 6 ]]
then
echo "Equal"
fi
}
Output:
{0..6}
0 1 2 3 4 5 6
Why is the output different whereas $1 and 6 is equal?
The curly-braces get expanded before $-variables are expanded, and the curly-brace range syntax simply doesn't recognize anything other than a pair of numbers with dots in between.

Check number of arguments in a script [duplicate]

This question already has answers here:
Check that there are at least two arguments given in a bash script
(2 answers)
Closed 1 year ago.
How can I check in my script if I have at least 3 arguments, it must be something like:
if #number_of_arguments < 3
then
echo "Not enough arguments"
if [ $# -lt 3 ]; then
...
fi
In bash an alternative is to use an arithmetic expression.
if (( $# < 3 )); then
...
fi

Why two numbers not summarized in script? [duplicate]

This question already has answers here:
How do I echo a sum of a variable and a number?
(9 answers)
How can I do basic maths in bash?
(4 answers)
How do I print the result of a command with 'echo'?
(1 answer)
Closed 4 years ago.
I am newer in bash.
I try to create script that summarizetwo numbers:
Here is script:
echo "the result is:" expr $1+$2
Here how I call the script:
./scr3 50 98
And here is result that I get after the script executed:
the result is: expr 50+98
While I get the the string with two summarized numbers I expect to get the summarize of two numbers.
My question is why I don't get the result of summarize of the two numbers?
Why? Because echo prints its arguments, and you're passing expr as an argument.
A best-practice alternative would be:
echo "The result is: $(( $1 + $2 ))"
...though the a smaller change (albeit to very inefficient code; expr is an artifact of the 1970s, made irrelevant with the introduction of $(( )) in the 1992 POSIX sh standard, and should never be used in new development) is simply:
echo "The result is: $( expr "$1" + "$2" )"

Shell scripting while loop error [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
#!/bin/bash
a=0
while ["$a" -lt 10]
do
a=`expr "$a" + 1`
echo $a
done
This is the error:
a.sh: 3: a.sh: [0: not found
you need to keep spaces around your brackets:
while [ "$a" -lt 10 ]
since bash interpretes strings; it uses spaces to separate words. When you write ["$a"; then bash reads: [0 (after $a is replaced with 0)

Bash Compare two variables, integer expression expected [duplicate]

This question already has answers here:
Bash: Integer expression expected [duplicate]
(2 answers)
Closed 7 years ago.
I have a problem with comparing 2 variables (one variable is read from file).
1 #!/bin/bash
2
3 CFR=$(<bubble_status.txt)
4 treshold=0.01
5 echo $CFR
6 echo $treshold
7
8 if [ "$CFR" -gt "$treshold" ]
9 then
10 echo "CFR is greater then treshold";
11 elif [ "$CFR" -lt "$treshold" ]
12 then
13 echo "CFR is less than treshold";
14 else
15 echo "Something else";
16 fi
But bash returns me this:
19.81
0.01
./dupa7.sh: line 8: [: 19.81: integer expression expected
./dupa7.sh: line 11: [: 19.81: integer expression expected
Something else
Any ideas?
Bash doesn't support float comparison. You could use bc:
if (( $(bc <<< "$CFR > $threshold") )); then
echo "CFR is greater then threshold"
fi
Bash doesn't support floating point arithmetic. Use another tool such as bc or awk if you want to do floating point comparison/math.

Resources