I'm getting an error when using a script in zsh, but the error only returns a small red x on my prompt instead of stderr. Using the script in bash returns no error and works as expected. I'm just using bare-bones zsh, no OMZ, etc.

Is there a way to output the error in zsh to give me an idea of where to look?

I don't think this has anything to do with the software I'm installing, but rather with zsh; however, I don't even know where to start where to look. More background information for those interested:

I installed a package commonly used for computational fluid dynamics (CFD) simulations called OpenFOAM. OpenFOAM is a project that has many different versions available, and since loading different versions simultaneously results in conflicting namespaces, it's common to use an alias to load a single version into the shell path. For example, my .zshrc file contains the following lines:

alias  of5x="source $HOME/OpenFOAM/OpenFOAM-5.x/etc/bashrc $FOAM_SETTINGS"
alias  of4x="source $HOME/OpenFOAM/OpenFOAM-4.x/etc/bashrc $FOAM_SETTINGS"
alias of30x="source $HOME/OpenFOAM/OpenFOAM-3.0.x/etc/bashrc $FOAM_SETTINGS"

When I use the alias to load the most recent version (5x), I'm greeted with a red x on my command line:

Screenshot of terminal


The script in question is meant to be usable from any POSIX-compatible shell as it says so itself:

#     Should be usable by any POSIX-compliant shell (eg, ksh)

The scripts in this part of OpenFOAM are simply divided in twain into scripts for C shells and scripts for POSIX-conformant shells. This particular script does very little apart from if, [, export, and unset and there is no issue of Bourne Again shell idiosyncrasies.

The simple truth is that the question incorrectly states

I'm just using bare-bones zsh

In fact, the questioner clearly has a PROMPT that prints a red cross if the exit status of the last command was non-zero (like this one from a powerline theme). Of course it is non-zero for this particular script, because the last command in the script is:

[ "$BASH" ] && . $WM_PROJECT_DIR/etc/config.sh/bash_completion

That yields a non-zero exit status for the script in the Z shell, which causes the questioner's prompt to display the red character. Ironically, this check was put in to ensure that the script runs happily with the Korn and Z shells; which it does, the red cross and the exit status being purely cosmetic problems.

The use of powerline private-use-area non-standard Unicode characters in the prompt is a giveaway that this is not the vanilla Z shell prompt in action here.

Further reading

  • Thanks for the incredieble answer. I regrettably don't know enough about bash/zsh to tell the whole story here: I was mistaken about my zsh setup, it's true that I'm using a couple plugins with the shell, namely: zsh-syntax-highlighting, and gruvbox_256palette color scheme. – cbcoutinho Oct 13 '17 at 12:39
  • Also, just for completion, commenting out the line you mentioned associated with bash completions solves the issue. Thanks again @JdeBP – cbcoutinho Oct 13 '17 at 12:59

zsh and bash are interpreters for two different languages, you can't expect zsh to be able to interpret any code written for bash, however you may be able to do:

alias of5x="emulate bash -c 'source $HOME/OpenFOAM/OpenFOAM-5.x/etc/bashrc $FOAM_SETTINGS'"

For zsh to enter a bash emulation mode for running that source command.

That emulate will reduce but not eliminate the differences between zsh and bash. Note that any function declared in that sourced file will retain that emulation mode.

In any case, the format for the prompt variable is completely different in bash and zsh, so I don't expect a script written exclusively for bash would manage to define a prompt that also works for zsh.

  • Or look in the OpenFOAM/OpenFOAM-5.x/etc directory  and see whether the package provides a zsh-compatible load script. – Scott Oct 13 '17 at 12:22

You could correct that script which exits with an error as described in JdeBP's answer:

Add true after the last line

[ "$BASH" ] && . $WM_PROJECT_DIR/etc/config.sh/bash_completion

Or an alternative could be to change that last line to

if [ "$BASH" ]; then . $WM_PROJECT_DIR/etc/config.sh/bash_completion; fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.