Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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]
\n\[\e[1;31m\]\u@\H

#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." 
fi\`"

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

share|improve this question

2 Answers 2

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 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.