I would like to write a small part of a script that saves the error status, executes some other code, and sets the error status to the original error status. In Bash it looks like this:<< some command >>; _EXIT=$?;( <<other code here>> ;exit $_EXIT)But I need code that will run no matter if it is being run under bash, zsh, csh or tcsh. I do not know which shell will be used in advance, because it is decided by the user. The user also decides << some command >>.It is safe to assume that << other code >> will work in all shells, but it is not safe to assume that you can write a file (so putting it into a file will not work).BackgroundGNU Parallel executes commands given by the user in the shell decided by the user. When the command is finished, GNU Parallel has some cleanup to do. At the same time GNU Parallel needs to remember the exit value of the command given by the user. The code run before the snippet above is the user given command. << other code >> is the cleanup code.
Compatibility scripting: Save $? for use later
bash;zsh;tcsh;csh
Use perl:perl -e '$a=shift; `<< other code >>`; exit $a' $? # Fails in cshEdit This works in csh, too (and zsh, tcsh, sh, pdksh, ksh93 - though not fish):perl -e '$a=shift; `<< other code >>`; $a=~s/h// and exit $a; exit shift' $?h $status