Betlista
Betlista

Reputation: 10549

saving return code and returning it in 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?

Upvotes: 11

Views: 13616

Answers (1)

fge
fge

Reputation: 121720

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.

Upvotes: 19

Related Questions