0

So I am a newbie in using Linux and I am following courses and blog posts related to linux to learn more and more about it. As I was searching about Linux Bashrc, what it is and how to use it, I came across a piece of code to change the color of the shell when a command fails to work properly. I read about the linux bashrc in the linked article, but my problem is the following lines.

This prompt changes the color if your last command failed to run successfully, but it also shortens long paths and contains the bash history number of each command for easy retrieval. This can be very helpful and efficient.

PROMPT_COMMAND='PS1="\[\033[0;33m\][\!]\'if [[ \$? = "0" ]];
then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]";

fi\'[\u.\h: \'if [[ \pwd|WC -c|tr -d " "' > 18 ]]; then echo

"\\W"; else echo "\\w"; fi\']\$\[\033[0m\] "; echo -ne

"\033]0; hostname -s':'pwed'\007"'

I write exactly as this code says but still I get nothing, what could be the problem? I'd appreciate it if anyone could help me.

2
  • That code is hopelessly garbled; check out this stackoverflow Q&A instead. Commented Feb 21, 2021 at 21:22
  • bashrc is not a Linux-specific concept: macOS uses (used) it as well and it worked exactly the same, other OSes can use it too (I have bashrc on Windows). Similarly, Linux doesn't depend on bashrc support at all and can work well without it (my other computer runs Linux and doesn't have bashrc). There's a lot of intermediate concepts between "Linux" and "bashrc" and making a leap like this will IMO leave you with a mishmash of semi-knowledge and misconceptions. I'd suggest learning bottom-up rather than top-down: try reading about shells (bash in particular) and environment variables first
    – gronostaj
    Commented Feb 21, 2021 at 22:28

1 Answer 1

0

what could be the problem?

It seems the code started as an unsophisticated cumbersome one-liner which actually worked. Then it was badly crippled by humans (someone wrote it down and somebody else retyped?) and(?) machines (some characters were translated?); not necessarily in this order.

Now it's garbage. Even in its original (supposedly working) form it was a frenzy of quoting and escaping. Probably this:

PROMPT_COMMAND='PS1="\[\033[0;33m\][\!]`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi`[\u.\h: `if [[ \`pwd|wc -c|tr -d " "\` > 18 ]]; then echo "\\W"; else echo "\\w"; fi`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"'

Converted to a less cumbersome piece of code:

_prompt_f() {

if [[ $? = "0" ]]; then
   PS1='\[\033[32m\]'
else
   PS1='\[\033[31m\]'
fi

PS1='\[\033[0;33m\][\!]'"$PS1"'[\u.\h: '

if [[ "$(pwd | wc -c)" > 18 ]]; then
   PS1="$PS1\W"
else
   PS1="$PS1\w"
fi

PS1="$PS1]\$\[\033[0m\] "

printf '\033]0;%s:%s\007' "$(hostname -s)" "$(pwd)"
}

PROMPT_COMMAND=_prompt_f

You must log in to answer this question.

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