Skip to main content
3 of 4
added 515 characters in body

Last exit status from within script without source

Supposing someone invokes a non-existent command, the only way I know to retrieve its error status from a script I execute right after is by using source

I.e., let's say this script's name is dummy where its contents are simply

#!/bin/bash

echo $?

I know I will only get the exit status I'm looking for (127) if I invoke source dummy, but is there another way to facilitate getting the exit status (preferably without using alias) from the last command from within the script, without using source so I could simply call dummy to get my desired behaviour?

I am simply trying to make a script to check for a typo in the previous command. This requires detecting the previous exit status to make sure there was a potential typo in the first place, so I need to be able to get the exit status of the last command from within the script. I am also hoping to minimize the amount of setup a user would have to do, so that a user could simple invoke dummy, for example, and the rest would be taken care of. This is my reason for trying to avoid source or ., or alias.