Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number greater than 255 in bash shell script

Tags:

bash

shell

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.

like image 627
José Aciole Avatar asked Jan 18 '13 00:01

José Aciole


People also ask

How do you write greater than in bash?

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.

How use greater than or equal to in shell script?

'>' 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.

What does [- Z $1 mean in bash?

$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.

What does $() mean bash?

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).


1 Answers

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.

like image 161
kojiro Avatar answered Nov 02 '22 09:11

kojiro