11

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?

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, sequence of func and e=$? did the trick, missing $ was just a typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.