How to return a number greater than 255 in
function returnnumber(){
x=256
return $x
}
returnnumber
echo "value: $?"
I wanted to type 256 and 256 return
But 256 return 0.
echo "enter two numbers"; read a b; echo "a=$a"; echo "b=$b"; if [ $a \> $b ]; then echo "a is greater than b"; else echo "b is greater than a"; fi; The problem is that it compares the number from the first digit on, i.e., 9 is bigger than 10, but 1 is greater than 09.
'>' Operator: Greater than operator return true if the first operand is greater than the second operand otherwise return false. '>=' Operator: Greater than or equal to operator returns true if first operand is greater than or equal to second operand otherwise returns false.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
You can't! Shell functions don't really return anything, they just exit with an exit status, like other UNIX commands. And like other UNIX commands, their exit status must be an unsigned byte.
If you want to store the output of a command (which includes functions), you can capture its standard output in the usual way.
d() {
echo 'hi'
}
x=$(d)
echo "$x"
hi
Here's Wooledge's wiki on getting values from commands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With