I would like to set a variable inside .bashrc
to be equal to the output of a command that may return an error code.
I do not know the implications of doing such a thing. Will everything else work correctly even if this command fails?
The code in .bashrc
currently is simply:
export MYVAR=$(my_dubious_command 2>/dev/null)
Is it safe to do this?
Best Answer
As l0b0 said, it's unlikely that your .bashrc would be running with
errexit
set, but you could take care of that situation by testing:The
export
command, as written, should return 0, in case any subsequent commands check$?
; you are not providing any invalid options to it, the variable name is a valid one, and you are not exporting a non-existent function.The case statement checks to see if
errexit
is set; if so, it temporarily turns it off in order to runmy_dubious_command
.