TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Detecting command failures in bash

11 pointsby redduck666over 15 years ago

3 comments

derobertover 15 years ago
Much better approach (assuming all your terminals use the same colors codes, you could of course use tput if not):<p><pre><code> RED="\[\033[31;1m\]" NORMAL="\[\033[0m\]" PS1_INSIDE='\u@\h:\w$bad\$ ' PS1="\` retcode=\$? ; if [ 0 -ne \$retcode ]; then bad=\" [\\\\\\\$?=\$retcode]\" echo \"$RED$PS1_INSIDE$NORMAL\" else bad=''; echo \"$PS1_INSIDE\"; fi; \`" unset RED NORMAL PS1_INSIDE </code></pre> Examples:<p><pre><code> anthony@Tao:~$ true anthony@Tao:~$ false anthony@Tao:~ [$?=1]$ # actually in bold red</code></pre>
steveitisover 15 years ago
This article describes any easy way to do this incorrectly.<p>It also has simple spelling errors, such as 'weather' for 'whether'.<p>It is NOT what I've come to expect from HN. Downvotes for everyone involved.<p><i></i> Oh. Right. I can't downvote. Damn.
gcbover 15 years ago
so un-unix.
评论 #1091689 未加载