0

I use the terminal and want to use a specific colour when writing commands.

For instance, suppose I write "history" on the command-line. I would like that the commands entered from the keyboard are colourised with a predefined colour.

In summary, all the commands entered by the user will show with the same colour.

enter image description here

Thus

history

grep -hir -C 8 "hello world" .

will show in blue.

Currently I have the following for PS1

PS1='\n\[\033[01;36m\]\u@\h:'
PS1=$PS1'\n+ \[\033[38;5;214m\]\w\[\033[00m\]\n'

Then I did the following

OPS0="$PS0" OPS1="$PS1"
t7="$(tput setaf 7)" t5="$(tput setaf 5)"
[[ -n "$PS1" ]] && PS0="\[$t7\]" PS1="\[$t7\]$PS1\[$t5\]"

I get two strange characters at the beginning, just before 01cuneus

pete@home:~$ ls
01cuneus  Admir          Documents   logs      Public
02chaos   conjgrad.f90~  Downloads   Music     temp
03marshl  Desktop        fontconfig  Pictures  Videos
8
  • You seem to have answered your own question with another question Commented Jun 25, 2021 at 22:04
  • This is somewhat different. The other referred to using echo. Now I am using printf and set a colour. In this question, I want to colourise the commands I write. Seppose I am in a terminal anh input the command "history" and want to use a particular colour. I would not be using echo in this case but a simple input from the keyboard. Commented Jun 25, 2021 at 22:08
  • OK. I think you need to be far clearer in this question what it is you're looking for. Or did you mean echo hello; tput setaf 4; echo world; tput setaf 7 Commented Jun 25, 2021 at 22:13
  • Yes you can use echo that way, using tput. But here I want to ask about inputting commands with a different colour. Commented Jun 25, 2021 at 22:18
  • Have now put an example Commented Jun 26, 2021 at 14:12

1 Answer 1

0

Reading the documentation for bash will lead you to the section on variables related to prompting. We are interested in these

  • PS0 The value of this parameter is expanded and displayed by interactive shells after reading a command and before the command is executed.
  • PS1 The value of this parameter is expanded and used as the primary prompt string.

Both variables reference the later section on PROMPTING, which has these two markers for use within a prompt string

  • \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • \] end a sequence of non-printing characters

Finally, the tput command references terminfo, which has these sections about colours

  • setab Set background color to #1, using ANSI escape
  • setaf Set foreground color to #1, using ANSI escape

and

The setaf/setab [...] capabilities take a single numeric argument each. Argument values 0-7 of setaf/setab are portably defined as follows [...]

Color       #define       Value     RGB
black     COLOR_BLACK       0     0, 0, 0
red       COLOR_RED         1     max,0,0
green     COLOR_GREEN       2     0,max,0
yellow    COLOR_YELLOW      3     max,max,0
blue      COLOR_BLUE        4     0,0,max
magenta   COLOR_MAGENTA     5     max,0,max
cyan      COLOR_CYAN        6     0,max,max
white     COLOR_WHITE       7     max,max,max

Putting this all together we can modify the existing PS1 and PS0 variables so that the colour is changed for the command input but restored before command execution:

OPS0="$PS0" OPS1="$PS1"                                       # Save originals
t7="$(tput setaf 7)" t6="$(tput setaf 6)"                     # White, Cyan
[[ -n "$PS1" ]] && PS0="\[$t7\]" PS1="\[$t7\]$PS1\[$t6\]"     # After and before command entry

If you don't like what you see you can restore the values (or simply start a new shell)

PS0="$OPS0" PS1="$OPS1"

When you're ready, add the lines to your .bashrc for permanent excitement.

6
  • Have added my current setup for PS1 (shown in my question). Which colourises username, hostname, and the current working directory. Commented Jun 26, 2021 at 18:21
  • Doesn't matter what your $PS1 is; this code will work with it regardless Commented Jun 26, 2021 at 18:26
  • Have appended [\033[01;32m] to the end of my current PS1. Commented Jun 26, 2021 at 18:35
  • Why would you do that instead of doing what I've recommended? Commented Jun 26, 2021 at 19:13
  • I am noticing some strange characters after doing ls. Commented Jun 26, 2021 at 19:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.