Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving return code and returning it in bash

Tags:

bash

I'd like to do this in bash

#!/bin/bash

func(){
    return 1;
}

e=func
echo some text
exit e

but I'm getting

exit: func: numeric argument required

AFAIK variables in bash are without a type, how to "convert" it to int to satisfy requirement?

like image 242
Betlista Avatar asked Jan 10 '13 20:01

Betlista


1 Answers

You need to add a $ in front of a variable to "dereference" it. Also, you must do this:

func
e=$?
# some commands
exit $e

$? contains the return code of the last executed "command"

Doing e=func sets string func to variable e.

like image 196
fge Avatar answered Oct 05 '22 07:10

fge