On 4/14/19 1:28 AM, Paul Wise wrote: > Hi folks, > > I wanted a way in bash to print the status of the last command I ran, > including information about each command in a pipe and about signals. > > I noticed that bash does not have the ability to display a command > result, so I came up with the attached prompt configuration. > > I am hoping that folks here are interested in fixing the things that I > found that I had to workaround: > > * The command number variable $# is not available at the time that the > PROMPT_COMMAND is run, only when $PS1 is evaluated.
That's the number of positional parameters. > * There isn't an easy way to detect no command run yet, syntax errors, > Ctrl+C, Enter etc versus a command being run and exiting Syntax errors set $? to 2. SIGINT at the prompt sets $? to 130. There's no way to determine whether or not those exit statuses differ from an exit status resulting from a command being run. > * Sometimes PIPESTATUS isn't reset even though $? has been reset PIPESTATUS doesn't get set until a job completes. $? will be reset to 128+SIGNUM when a job stops, since that is what shells do and it seems reasonable as a way to let the user know a job suspended. It doesn't seem particularly useful to set PIPESTATUS to, e.g., {146, 146, 146} in this case, though. > > I am hoping that folks here have suggestions or fixes for the > things that I found I wasn't able to do: > > * Determine if a command exited normally or due to a signal, even if > it exited normally with an exit code greater than 128. There is no way to distinguish these cases. There is a lot of convention at work here, and convention is that well-behaved programs don't exit with a status greater than 128. > * Have the PIPESTATUS be correct after a Ctrl+Z. See above. You could add logic if $? == 128+SIGTSTP. > * Differentiate between Ctrl+C Ctrl+C and Ctrl+C Enter. The tool you have is the exit status of the last command. From that perspective, there's no difference. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU c...@case.edu http://tiswww.cwru.edu/~chet/