2

How to edit $PS1 variable so that the output of a bash command is another color than the command? For example, I want the command to be black, and the output to be green.

username$ seq 1 3
1
2
3

In the above example, I would want seq 1 3 to be black, and the output numbers to be green.

This is my current $PS1 variable:

\[\e[1;0:31m\]\u \[\e[m\] \[\e[1;0:36m\] \@ \[\e[m\] \[\e[1;0:32m\]\W $ \[\e[m\]
1

1 Answer 1

2

You should make use of trap "changesColorHere" DEBUG and PS1. In the PS1 variable you can specify at the end of its value the color and what trap "" DEBUG does is to run a command immediately before another is executed. So, if you run df the command specified in trap "" DEBUG will before df.

You can set PS1 variable to this:

PS1="\[\e[1;0:31m\]\u \[\e[m\] \[\e[1;0:36m\] \@ \[\e[m\] \[\e[1;0:32m\]\W $ \[\e[1;35m\]"

With this setting all input you type after the prompt will be colored.

And you can set trap "" DEBUG to something like this:

trap "echo -ne '\e[1;33m'"  DEBUG

With this setting the output of almost every command you type will be colored.

Some commands such ls,dmesg,journcaltcl, etc will show the output with the colors they are configured. So you have to disable them by using:

  1. When you use ls this command is usually an alias which has the option --color=tty. So to be able to print the ls output with your custom color you can use:
\ls #Add `\` before the ls command
ls --color=never ... # Add the option --color=never
  1. The dmesg command prints the output with its custom colors but you can prevent to use them and use the yours instead:
dmesg --color=never #Add --color=never
  1. journalctl also prints its output with custom colors. Although this can be disabled by exporting export SYSTEMD_COLORS=false if you simply use journalctl the output will make use of the less command. So I suggest you use:
export SYSTEMD_COLORS=false
journalctl  --no-pager
#Alternatively you can use:
journalctl | cat #This should be useful if there were commands whose page their
# output through `less` and they do not have an option like `--no-pager`

Using the settings above I have:

enter image description here

You must log in to answer this question.

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