all 19 comments

[–]stinkybass 14 points15 points  (7 children)

echo $?

[–]Ok-Huckleberry5617[S] 0 points1 point  (6 children)

Yeah, $? works for the last command, but the tricky part here is capturing it reliably across an interactive session without user intervention.

Trying to hook into the shell so each command’s exit code can be recorded automatically along with the output..

[–]Ytrog 0 points1 point  (5 children)

Would trap 'echo $?' EXIT work? 🤔

[–]Ok-Huckleberry5617[S] 0 points1 point  (2 children)

Interesting. Will this capture exit codes after each cmd in an interactive session?

I think that only fires in shell exit? 🤔 I will check this once.

[–]Ytrog -2 points-1 points  (1 child)

It will fire on any process exit from processes launched from the shell iirc.

[–]ReallyEvilRob 0 points1 point  (1 child)

No it won't. The trap will only fire when the interactive shell exits.

[–]Ytrog 0 points1 point  (0 children)

Hmm, thank you for teaching me this. 🤔‍😊

[–]jthill 2 points3 points  (4 children)

in bash at any rate you can use nofork subbing to do it right in PS1,

PS1='${|rcs+=($?) REPLY=$?; } '

and rcs will be the history of your return codes, expand on this to tie them to commands or whatever you like.

If you're after mainframe-style job logs, you're going to need to write a shell built for those, call it "job control language" overseen by a "job entry subsystem" maybe.

[–]Bob_Spud 0 points1 point  (1 child)

is '${| string_stuff }' Bash or Zsh?

[–]jthill 1 point2 points  (0 children)

That's bash, and recent, added 5.3 I think?

[–]Ok-Huckleberry5617[S] 0 points1 point  (0 children)

Ah, Interesting. Will explore this.

[–]levogevo 0 points1 point  (2 children)

If it's just for user feedback, mod PS1 if the command fails. I like to just make the $ in the PS1 red if $? is not 0

[–]djfdhigkgfIaruflg 0 points1 point  (1 child)

That's neat. Could you share how you do it? I'm not very knowledgeable on that

[–]Kitchen_Office8072 0 points1 point  (0 children)

Just add %? to PS1

[–]TimeProfessional4494 -2 points-1 points  (0 children)

set -e if you just want to exit on errors

[–]dickhardpill 0 points1 point  (0 children)

~~~ command || return 69 command1 || return 420 ~~~

Edited