Is the "$?" (dollar-question mark) variable available only in the Bash shell?
The $?
exit-code is common to any shell that follows POSIX, and is described in 2.5.2 Special Parameters:
?
Expands to the decimal exit status of the most recent pipeline (see Pipelines).
As Thomas Dickey said, any POSIX shell (ie. pretty much all of them) will have $?
.
This question interested me quite a bit, so I tested it on any shell I could get my hands on:
mksh
zsh
/bin/sh
on my Samsung Galaxy S5/bin/sh
on my routertcsh
ksh
dash
/bin/sh
on my virtual UNIX System V from 1989 or socmd.exe
andpowershell.exe
on my Windows 10 computer
and $?
worked in all of these but fish
and cmd.exe
.
Found two interesting things:
1. $?
works in Windows PowerShell!
Well, to a point. Instead of returning 0 or a higher number, it's just True
and False
.
PS C:\WINDOWS\system32> echo $?
True
PS C:\WINDOWS\system32> gfdhgfhfdgfdg
gfdhgfhfdgfdg : The term 'gfdhgfhfdgfdg' is not recognized as the name of a cmdlet, ...(big long error output).....
PS C:\WINDOWS\system32> echo $?
False
2. $?
doesn't work in the shell fish
.
However, when you type $?
in fish, you get this message:
~$ echo $?
$? is not the exit status. In fish, please use $status.
fish: echo $?
I haven't used it much but I'm not surprised, fish
seems to have its own interesting shell language, completely different from bash
or whatever.