Note that there are some explanatory texts on larger screens.

plurals
  1. POComparison to exit status not working
    primarykey
    data
    text
    <p>I'm trying to customize my bash prompt. I want it so change color if I enter a command that doesn't exist, or for some other reason fails. I'm fairly new with coding bash scripts, so I looked at some other scripts so get some help, but something's not working. Here's my code:</p> <pre><code>export PROMPT_COMMAND=" PS1='` if [ $? -eq 0 ]; then echo ' \[\033[00;32m\]┌─\[\033[00;35m\]\u\[\033[01;32m\]@ \[\033[00;35m\]\h\n\[\033[00;32m\]└─\[\033[01;32m\] (\[\033[01;35m\]\W\[\033[01;32m\])\[\033[00;32m\]\$'; else echo ' \[\033[00;31m\]┌─\[\033[00;35m\]\u\[\033[01;31m\]@ \[\033[00;35m\]\h\n\[\033[00;31m\]└─\[\033[01;31m\] (\[\033[35m\]\W\[\033[31m\])\[\033[00;31m\]\$'; fi`\[\033[0m\]'" </code></pre> <p>I do not have any linebreaks in the actual code, since that would be messy with PS1, I just added them to make the code easier to read. </p> <p>So, I want to compare the exit status, <strong>$?</strong>, to <em>0</em>. For some reason the variable $? doesn't change at all in the script. It just remains <em>0</em>, so the first condition is always true, even when I issue a faulty command. I've tried adding <em>echo $?</em> to the code before the if-case, and this always returns 0, even if issuing <em>echo $?</em> as a command to the terminal returns something different. I've tried copying working code into mine, but that doesn't solve it either. Also, it worked when I used <strong>'</strong> as the outer citation and <strong>"</strong> as the second. I changed this because the script wouldn't accept <strong>(</strong> as a character otherwise.</p> <p>Any ideas why this is and how I can fix it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload