Is the "$?" (dollar-question mark) variable available only in the Bash shell?
Solution 1
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).
Solution 2
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 router tcsh
ksh
dash
-
/bin/sh
on my virtual UNIX System V from 1989 or so -
cmd.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.
Related videos on Youtube
patricK
Updated on September 18, 2022Comments
-
patricK over 1 year
I was following the example this blog and the question arose me after implementing
https://github.com/DanialK/ReactJS-Realtime-Chat
Summarizing, before send a message via websocket the state of messages is updated. And when server receives that message, they send a broadcast to all clients, indluding myself. Thereat, client updates the state with this same message
Why this message does not appear 2 times? I don't want that message appear 2 times, but I want to know why it happens
Client code:
socket.on('send:message', this.messageRecieve); ... handleMessageSubmit : function(message){ Messages.push(message); this.setState({ messages : Messages }); socket.emit('send:message', message); }, messageRecieve: function(message){ Messages.push(message); this.setState({ messages : Messages }); },
Server code:
socket.on('send:message', function (data) { socket.broadcast.emit('send:message', { user: name, text: data.text }); });
-
patricK over 9 yearsI don't receive me own message?
-
Sebastien Lorber over 9 yearsWe don't know if you receive this message, look in your logs and tell us. We can't know if this is a React render problem or a Websocket/server problem...
-
patricK over 9 yearsI asked this because I suspected that this would be a behavior of socket.io...
-
Admin over 8 yearsYou can use it in any POSIX shell, it's one of special parameters
-
-
patricK over 9 yearsI didn't found at socket.io docs it, however it is how it works, that explains my doubts, thank you
-
Stéphane Chazelas over 8 yearsNot only POSIX shell, all Bourne-like shells including the Bourne shell (which I believe was the one introducing it, the Mashey shell had it as
$r
I believe). So that's the sh of virtually all Unix-like systems since Unix V7 in the late 70s, Most other shells (csh, tcsh, fish, rc) have it as$status
. -
Stéphane Chazelas over 8 yearsMost shells (fish, csh, tcsh, rc, zsh) use
$status
which is a lot more straightforward/legible IMO. Only Bourne-like shells (among Unix shells) use$?
AFAIK. -
Admin over 8 yearsJust depends on how you are reading. I read mentally
$?
as "sh## happened?" and after that, i never forgot the meaning of this special variable :) -
OrangeDog over 8 yearsAnd many scripting languages: Perl, Ruby, etc.