June 2017
Beginner
502 pages
11h 26m
English
The test operator can be used in a few different scenarios. We already saw in the parameter substitution that it is used to check whether a variable has a value or not. In arithmetic operations, it can be used to implement the trinary or ternary operator in a C-style notation:
#!/bin/bashx=20y=30w=40z=50k=100echo 'Usually you would write a control loop in the following way:'echo 'if [[ $x -gt $y ]]'echo ' then'echo ' z="$w"'echo ' echo "The value for z is: $z"'echo ' else'echo ' z="$k"'echo ' echo "The value for z is: $z"'echo 'fi'if [[ $x -gt $y ]] then z="$w" echo "The value for z is: $z" else z="$k" echo "The value for z is: $z"fiecho 'But you can also use the C-style trinary operator to achieve the same result:'echo ...
Read now
Unlock full access