←back to thread

217 points tanelpoder | 8 comments | | HN request time: 0s | source | bottom
Show context
scudd ◴[] No.26492906[source]
TIL: You can use `echo $?` to get the last process's exit code.
replies(2): >>26493051 #>>26493264 #
1. enriquto ◴[] No.26493051[source]
this is actually the most useful thing to put in your prompt (much more than your username, the current directory and other redundant stuff), as in PS1='$?\$ '
replies(2): >>26495670 #>>26497266 #
2. chriswarbo ◴[] No.26495670[source]
I used to leave my prompt alone until recently, but was inspired by https://news.ycombinator.com/item?id=26059023 to at least conservatively raise my expectations.

Now my prompts start with the current UNIX time, coloured red if the previous exit code was non-zero, or green if it was zero. This gives me instant feedback on success/failure, and lets me retroactively see how long a command took (without having to re-run it with `time`).

replies(1): >>26496293 #
3. enriquto ◴[] No.26496293[source]
wouldn't it be more useful to show the time difference? Or maybe only show it in case it is larger than 1 second?
replies(2): >>26497304 #>>26500232 #
4. Noumenon72 ◴[] No.26497266[source]
Might be useful but I just spent half an hour trying everything at https://stackoverflow.com/questions/16715103/bash-prompt-wit... on a MacBook Pro and none of it worked.
replies(2): >>26497907 #>>26498559 #
5. Izkata ◴[] No.26497304{3}[source]
I do that, if it's over 60 seconds - it's not as straightforward since you also need to use PROMPT_COMMAND, a debug trap, and a variable to store the start time: https://jakemccrary.com/blog/2015/05/03/put-the-last-command...
6. LeoPanthera ◴[] No.26497907[source]
Maybe you are not using bash. echo $SHELL to find out.
7. JdeBP ◴[] No.26498559[source]
That's because, as a MacOS user, you are probably using the Z shell, not the Bourne Again shell. Prompt syntax is specific to each individual shell program.

One cannot just blindly use a Stack Exchange Q&A that states that it is for "Linux" and "bash" on the Z shell on MacOS, or indeed on the Z shell on a Linux-based operating system for that matter. Or in the C shell. Or in the Korn shell. Or in the Watanabe shell.

8. chriswarbo ◴[] No.26500232{3}[source]
Duration and start time can both be useful, and the former can be calculated from the latter as needed. Plus it's easier to implement (stateless) and has consistent alignment (always 10 digits).

BTW here's the function I use:

``` showTime() { if [[ "$?" -eq 0 ]] then TIMECOLOR='\e[92m' else TIMECOLOR='\e[91m' fi echo -e "$TIMECOLOR$(date '+%s')\e[0m" } ```