Ubuntu garbled text appears when I type

Ubuntu help

In this article I would like to show you how you can adapt the prompt on your system.
A prompt is the line that you can see when you open a shell. For example, it looks like this:

root @ ubuntu: / home #

Since we are individualists, we would of course like to adapt the prompt to our ideas.

Prompt basics

The current configuration of the prompt is saved in a variable. The variable is: "PS1".
You can also check that, just open a shell and enter the following:

[email protected]: ~ $ echo $ PS1
$ {debian_chroot: + ($ debian_chroot)} \ [\ 033 [01; 32m \] \ [email protected] \ [\ 033 [00m \]: \ [\ 033 [01; 34m \] \ w \ [\ 033 [00m \] \ $

The output can look different for you.
So we now know in which variable the configuration is saved.
If you now have a little knowledge of programming languages, then you know that variables can be defined and that is exactly what we want to do now.
For fun, type the following into the shell:

PS1 = "

As you can easily see, the prompt disappears completely. A logical reaction as we have removed all configuration instructions. (By the way: You can of course still type commands into the shell in this state)

In the event that you now panic because your standard prompt has disappeared, then I can calm you down. Just close the shell and open a new one and the prompt should come back.

We will deal with the permanent storage of the prompt below. First of all, we want to create a nice prompt, and this is where temporary storage comes in handy.

If we now look again at the predefined prompt, we will see many such character strings: "\ u", these strings are called sequences.

You can find a small overview of the most common and, in my opinion, most useful sequences in the following table (you can find a larger compilation here):

characterExplanation
\ dDate in the following format: "Mon Apr 27"
\HThe host, i.e. the computer name (something like 'mycomputer')
\ jThe sum of the commands canceled with CTRL + Z.
\ l
\ nNew line, for multiline prompts
\ r
\ sThe name of the shell (e.g. "bash"), in the broadest sense also the version.
\ tThe time in 24-hour format: "21:26:04"
\ TThe time in 12-hour format: "09:26:46"
\@The time in 12-hour format with am / pm:
\ uyour username
\ vThe version of your bash e.g. "3.2"
\ VThe version of your bash with the corresponding lease level, e.g. "3.2.39"
\ wThe directory you are in with the path
\ WThe directory you are in, without a path
\!The number of commands stored in the history buffer
\#The number of commands during a shell session
\$If you are root a "#" appears, otherwise "$"
\\A backslash

Now we can make a handsome prompt! We'll do that immediately, then we'll add a little color to the game.

I suggest the following sequences as a basis: \ t, \ u and \ w.
\ t tells us the time, \ u which user we are logged in with and \ w which directory we are currently in.
I suggest we wrap the prompt in square brackets and separate the respective sequences with: and @.
The whole thing looks like this:

PS1 = '[\ t: \ u @ \ w] ‘

For clarification: You can use all possible characters between the sequences, so you can, for example, define the following as a prompt: PS1 = "I'm the best of all: \ u". None of this is a mistake. All you have to do, if you want to have backslashes displayed, is to type a double backslash: "\". That was all, there are no more rules.

This is what the prompt looks like so far:

[18: 38: 08: ubuntu @ ~]

The display will differ for you because you did not try the Confidence prompt at the same time and you may have called your host differently.

Color the prompt

First, I'll show you a picture that shows you all the available colors. The script that generated this ad can be found here.

The left values ​​(30-37) are responsible for the text coloring. The 1 indicates whether fat or not.
The top values ​​(40-47) for background staining.

Coloring the prompt is actually very easy. Activate the color mode with "\ e", followed by square brackets "[", then come the color values ​​and after the last color value there is an "m", if you want the text to be bold, you have to add a 1m at the end append. The "m" in front of the background value must then be removed. That's it. Incidentally, the text color value and background color value can be interchanged at will, so the order does not matter.

The syntax generalized (do not use):

\ e [[text color value]; [background color value] m
or for fat
\ e [[text color value]; [background color value]; 1m

Firstly, this notation is not completely clean and secondly, this method colors everything red, including the commands that are entered afterwards.

First we take care of the reset to the normal colors, for this we simply write the following "\ e [0m" at the point where we want to end the coloring.

Now we come to the problem of cleanliness. So that our prompt is properly defined, we need these two sequences "\ [" and "\]". They also include the text, if you don't set them then your prompt will fail with some commands.

So the absolute generalized form is this:

\ [\ e [[text color value]; [background color value] m \] (text / sequences) \ [\ e [0m \]
or for fat:
\ [\ e [[text color value]; [background color value]; 1m \] (text / sequences) \ [\ e [0m \]

If you're a little confused now, the following examples will hopefully make it a little clearer.

So that you can better understand my prompt later, I will tell you in this small table in which colors I would like to color my respective sequences or separators.

Sequence / textcolour
[ or. ]Blue, fat
\ tPurple, fat
\ uRed, fat
\ wgreen
@Red, fat
:Blue, fat

The prompt converted into the "prompt syntax" then looks like this:

PS1 = '\ [\ e [34; 1m \] [\ [\ e [0m \] \ [\ e [35; 1m \] \ t \ [\ e [0m \] \ [\ e [34; 1m \]: \ [\ e [0m \] \ [\ e [31; 1m \] \ u \ [\ e [0m \] \ [\ e [31; 1m \] @ \ [\ e [0m \] \ [\ e [32m \] \ w \ [\ e [0m \] \ [\ e [34; 1m \]] \ [\ e [0m \] '

I admit it looks very chaotic, that's why it's broken up a little:

\ [\ e [34; 1m \]
[
\ [\ e [0m \]

\ [\ e [35; 1m \]
\ t
\ [\ e [0m \]

\ [\ e [34; 1m \]
:
\ [\ e [0m \]

\ [\ e [31; 1m \]
\ u
\ [\ e [0m \]

\ [\ e [31; 1m \]
@
\ [\ e [0m \]

\ [\ e [32m \]
\ w
\ [\ e [0m \]

\ [\ e [34; 1m \]
]
\ [\ e [0m \]

Unfortunately, you cannot type it into the shell like this, because otherwise it will give you line breaks.

It is sometimes unnecessary to set the color to zero again and again, but in the end it is much more practical, because if you don't set everything back to 0 and have previously marked a sequence in red and bold and want to change the color for the next sequence and this sequence do not want to have fat, then you get a problem because at this point you first have to set it back to 0 and then set the parameters again. It works, there are fewer lines, but I find it cleaner that way.

In the end, the prompt should look something like this:

Save the prompt permanently

To save the prompt permanently we have to edit our .bashrc. You can find the .bashrc in your home folder.
Opens a terminal.
Type in the following:

cd ~ / && nano .bashrc

Move to the end of the file and just write the following there:

PS1 = '[your configuration] ‘

You have to swap your configuration with your configuration, of course, if you want to use mine then write the following at the end of the file:

PS1 = '\ [\ e [34; 1m \] [\ [\ e [0m \] \ [\ e [35; 1m \] \ t \ [\ e [0m \] \ [\ e [34; 1m \]: \ [\ e [0m \] \ [\ e [31; 1m \] \ u \ [\ e [0m \] \ [\ e [31; 1m \] @ \ [\ e [0m \] \ [\ e [32m \] \ w \ [\ e [0m \] \ [\ e [34; 1m \]] \ [\ e [0m \] '

If you look at the file then you will already find a prompt configuration there, we will simply overwrite it with the technique explained above. This gives us the advantage that if we no longer like the prompt, we can easily return to the standard prompt.

This post was published on by admin in Basics. Keywords: bashrc, färben, prompt, ps1.