I had a problem with testing $?inside a function, regardless of how it is passed.
__myretval () {
if [ $1 -ne 0 ]; then
printf -- "%s" "$1"
fi
}
PS1="$(__myretval '$?') $"
The goal is to show that they are not equal to 0. The MUCH function is more detailed than this, and it should be in the function, please do not offer to deduce this from the function.
$ false
1 $ true
$
I have tried every combination that I can think of, but nothing works, including, but not limited to, the following combinations. I tried putting the value in quotation marks and without quotation marks; I tried to do the same for 0, with quotes and without.
if [ $1 -ne 0 ]; then
if [ $1 != 0 ]; then
if [ $? -ne 0 ]; then
if [ $? != 0 ]; then
PS1="$(__myretval "$?") $"
PS1="$(__myretval "\$?") $"
Either the value always prints, or never prints.
source
share