Bash Command Line Colors

Bash provides a color feature for certain file types. For example, by default, ls will show directories as blue, utilities green, files white, etc. I know that these colors can be configured to output commands, such as ls, by changing ~ / .dir_colors and other files depending on how widespread the change should be made to the system.

However, during screencasts and presentations, I saw that on the command line itself There are these color modifications. For example, in a bash prompt, if someone enters a utility and starts to enter their arguments, the utility on the line turns green, as it would be in the output of ls. This seems like a nice feature, but all of Google’s search queries look like changing ls outputs and adjusting colors, but not how to apply this to the command line itself. How can this be applied to the command line? Is this possible via bash or is it a feature of some terminal software that these users use?

+6
source share
3 answers

fish - a friendly interactive shell https://en.wikipedia.org/wiki/Friendly_interactive_shell looks the way it reacts when you describe.

+1
source

Not sure how useful this is, but the tput command can help change the colors and format the command line.

For example, to change the color of the text in the terminal to red, you can do.

tput setaf 1 

This takes numerical arguments between 1 and 7. Then it can be added to ~/.bash_profile or ~/.bashrc to set colors when a user logs in or when starting a new terminal session.

Along with changing colors, tput has the ability to make text bold, underlined and more.

For more use, check out these pages:

TDLP - http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html

Hack40- http://linux.101hacks.com/ps1-examples/prompt-color-using-tput/

-1
source

Oh yes, I always do this for every Mac and Linux server I have. Not only can you provide useful information such as "user @ server-name: working / directory", but it's just fun!

I like it when they tell me which Git branch I'm working on when I enter the Git repository!

You just need to add one change to ~/.bash_profile or ~/.bash_rc

Here is a website to help you generate complex code: https://www.kirsle.net/wizards/ps1.html

And just you have an example, here is what I use on my personal Mac (without detecting the Git branch):

 PS1='\[\033[02;36m\][\ u@MBP :\[\033[02;33m\]\w]\$\[\033[00m\] ' 

The above code will give you:

  • The vertex color showing the user and the static host name: "user @MBP". You can rename the host name to whatever you like.
  • Then the yellow "current working directory"
  • Finally, it is followed by the '$' character and resets the colors back to white.

Setting colors for different parts becomes difficult. I definitely recommend using a generator. But if you want to make your own footprint, here is a list of all the codes for the bash colors: http://blog.bigdinosaur.org/easy-ps1-colors/

-1
source

Source: https://habr.com/ru/post/978767/


All Articles