VARS=$( # this allows to capture subshell stdout to a variable
. $HOME/script.sh 1>/dev/null # this prevents script printing something to stdout
set # this prints all variables to stdout
alias | sed 's/.*/alias \0/' # this prints aliases to stdout and prefixes them with "alias "
) || ERRCODE="$?"
if [ $ERRCODE==0 ]
then
eval $VARS # if everything is ok, we reevaluate the variables and aliases in this shell
fi
###
Now the "VARS=$(command) || ERRCODE="$?"", || prevents the command from exiting the script if the next command exits with code 0. We could also use "VARS=$(command) || true", but then "$?" would contain the result of command "true", since ERRCODE="$?" also exits with code 0 we can prevent the shell from exiting and remember the correct error code.
The idea is simple:
###
VARS=$( # this allows to capture subshell stdout to a variable
. $HOME/script.sh 1>/dev/null # this prevents script printing something to stdout
set # this prints all variables to stdout
alias | sed 's/.*/alias \0/' # this prints aliases to stdout and prefixes them with "alias "
) || ERRCODE="$?"
if [ $ERRCODE==0 ]
then
eval $VARS # if everything is ok, we reevaluate the variables and aliases in this shell
fi
###
Now the "VARS=$(command) || ERRCODE="$?"", || prevents the command from exiting the script if the next command exits with code 0. We could also use "VARS=$(command) || true", but then "$?" would contain the result of command "true", since ERRCODE="$?" also exits with code 0 we can prevent the shell from exiting and remember the correct error code.