Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I made a custom PS1 variable, which is a tremendously confusing. Here is the one that is closest to being fully working:

PS1="\n[\e[1;31m]\u@\H[\e[35m] \@ [\e[32m] PWD: \w [\e[1;34m]This folder has \$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files [\e[1;33m]A total of \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\n`if [ \$? = 0 ]; then echo [\e[32m]^_^ [\e[0m] Worked - [\e[0m]; else echo [\e[31m]O_O[\e[0m]Didn't worked - ; fi`"

This is the same code as the previous code sample, but it has been separated into blocks and commented for better readability:

#User@Host [Red]

#Hour [Purple]
\[\e[35m\] \@ 

#PWD [Green]
\[\e[32m\] PWD: \w 

#Number of files in PWD [Blue]
\[\e[1;34m\]This folder has \$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files 

#Amount of space the PWD files take, also line break [Yellow]
\[\e[1;33m\]A total of \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\n

#Malfunctioning condition, should smile if the instruction went right, or poker-face if not [green and red]
\`if [ \$? = 0 ]; then echo \[\e[32m\]^_^ \[\e[0m\]\[\e[0m\]Worked - \[\e[0m\]; else echo \[\e[31m\]O_O\[\e[0m\] Didn't worked - ; fi\`

Trying to explain the problem:

  • if looks for a condition that can no longer be false after running for (eg.) "This folder has \$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files", as it is true if the latest statement had a clean end.

How can I make it work while at same time keep the output in that same position? If the if is moved to before the first command executed, it works fine.

I thought of assigning a variable with that if condition at the start of the PS1. Then other if at this one's position would judge the alternative variable that would not change. But unfortunately I have no skill at all with Bash. I have already tried unsuccessfully to do it a thousand or so times.

Something like this (but well written):

PS1="`if [ \$? = 0 ];then echo "prev_err=0"; else prev_err=0; fi\` 
...[Some more code in between]...
`if [ \$prev_err = 0 ]
    then echo "No error"
    else echo "There was an error in the statement." 

Some ` (backtick characters) are in the wrong places or omitted in the previous code block.

share|improve this question
up vote 2 down vote accepted

You are probably better off doing most of that in PROMPT_COMMAND which, to quote the Bash manual "If set, the value is executed as a command prior to issuing each primary prompt"

See also this huge example.

Mine is :-

PROMPT_COMMAND='history -a; history -n; printf "\e]1;${PWD}\a"'

which keeps my history synched across terminal windows.

Then you should probably put as much of possible inside bash functions just to simplify the problem.

BTW - you might find du -sh . is a little more useful and easier than /bin/ls -lah | /bin/grep -m 1 total just to get the file usage`

share|improve this answer
I've loved all that I've seen, mind you (especially the linked huge example), but that last part sounds like it could lead to severe lags. For instance, timing a du -sh . in $HOME took real 0m34.018s, and had 4 du: ./Library/...: Permission denied' diagnostics. Timing the latter case, real 0m0.006s - hardly noticeable. – Bill McCloskey Jan 18 '14 at 15:28

My prompt contains several functions, such as

PS1='$(exit_code=$?; [[ $exit_code -eq 0 ]] || printf %s \[$BOLD_FORMAT\] \[$ERROR_FORMAT\] $exit_code \[$RESET_FORMAT\] " ")'

Does that work for you?

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .