0

I have my bash prompt on one line colored green with file path in blue. When I type a command it appears on the next line. After I press enter the output appears on the next line(s). Then there is an empty line.

I would really like the command to be in a color of my choosing (preferably not green or blue) or bold to differentiate it from the line before it and output line(s) after it. I do not want to alter the output color as that is used to indicate different things like executables and different types of links.

In the example in the image I would like 'ls -la var' to be a different color. Any advice would be very welcome.

enter image description here

EDIT: Based on the answer from don_aman, I added these two lines to my .bashrc file:

PS1="\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \n\\[\\e[1;33m\\]"

and

PS0='\[\e[0m\]'

Without the second line some lines of the output were also colored the same as the command.

Now my terminal looks like this which helps me to differentiate between the command and the output: enter image description here

1 Answer 1

3

You can control the format of any text following the prompt by changing the prompt itself, which is defined in the PS1 variable in Bash. I don't really understand terminals, but control sequences listed in console_codes(4) work for XTerm which I guess is the type of your terminal (check TERM environment variable), refer to that man page to add any desired customizations to your prompt, more specifically the ESC [ parameters m sequence, which allows to set display attributes of the terminal.

To change PS1, search for it in your ~/.bashrc file, then append whatever sequences you like. For example, on my system PS1 is initially set to the following value.

PS1="\\[\\e]0;\\u@\\h: \\w\\a\\]\${debian_chroot:+(\$debian_chroot)}\\[\\033[01;34m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;32m\\]\\w\\[\\033[00m\\]\\\$ "

If I wanted to make the input text bold and brown I'd have to add the sequence \[\e1;33m\] to PS1; here \[ begins a sequence of non-printing characters (check bash(1)), and \] ends it, and I'm using the previously mentioned display attribute control sequence, using the parameters 1, which sets bold, and 33, which sets brown foreground, separated by semicolons. Finally, I change the PS1 assignment in .bashrc to:

PS1="\\[\\e]0;\\u@\\h: \\w\\a\\]\${debian_chroot:+(\$debian_chroot)}\\[\\033[01;34m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;32m\\]\\w\\[\\033[00m\\]\\\$ \\[\\e[1;33m\\]"

Additionally you may want to reset the display attributes before executing commands, which can be done using the PS0 variable. The 0 parameter for the same sequence shown before resets all attributes.

PS0='\[\e[0m\]'

2
  • And what do you think makes it a second line?
    – don_aman
    Jul 20, 2022 at 15:52
  • I am pleased to say @don_amin's answer has provided the solution. I have updated my question to show exactly what I added to .bashrc and uploaded an image of the result. I have to admit I blindly copied and pasted with a bit of trial and error. I will try and understand the logic now... Jul 20, 2022 at 16:01

You must log in to answer this question.

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