Quick Tips and Tricks in Bash

Anyone who started in Linux terminal is familiar with the default line in Bash:

[[email protected]$host ~]$

But did you know that it is completely customizable and may contain some useful information? Here are some hidden gems you can use to customize your Bash string.

How do I set the command line in Bash?

The string in Bach is set by an environment variable PS1 (prompt line 1), which is used for interactive shell prompts. There is also a variable PS2which is used in case of input and required to execute a Bash command

[[email protected] ~]$ export PS1="[Linux Rulez]$ "
[Linux Rulez] export PS2="... "
[Linux Rulez] if true; then
... echo "Success!"
... fi

Where is the PS1 value set?

PS1 is a regular environment variable.

The system default is set to / etc / bashrc… On our system, the default prompt is set with this line:

[ "$PS1" = "\s-\v\$ " ] && PS1="[[email protected] W]\$ "

It checks if the value is PS1: s- v $ (system default), and if there is one, it sets PS1 to [[email protected] W]\ $

If you want to see a custom tooltip, you shouldn’t edit / etc / bashrc… Instead, you must add it to .bashrc in your home directories.

What does u, h, W, s, and v mean?

In the Hints part man bash, you can find a description of all special characters in PS1 and PS2 … Below are the default parameters:

  • u : Username
  • h : Short hostname
  • W : Base name of the current working directory ( ~ for home, current directory elsewhere)
  • s : Shell name (Bach or sh, depending on what the shell is called)
  • v : shell version

What other special strings can I use in tooltips?

There are a number of special lines that can be useful.

  • d : Expands dates in “Tue Jun 27” format
  • D {fmt} : Allows custom date formats – see man strftime for available options
  • D {% s} : Shows the date and time in the current locale
  • n: Include a newline (see multiline hints below)
  • w: Full path to the current working directory
  • H : Fully qualified hostname for the current machine
  • ! : Number history – you can run any previous command with its number history using the event shell history target pointer, ! and then the number for the specific command you need.

There are many other special characters, you can see the full list at PROMPTING parts Bash man page

Multichannel hints

If you are using longer prompts (for example if you include H or w or full date and time), you can split things into two lines. Here is an example of multi-line strings a, with the date, time and current working directory on one line, and username @hostname on the second line:

PS1="D{%c} wn[[email protected]]$ "

Are there any other interesting things we can do?

One thing people sometimes do is create colorful invitations. While we find it annoying and distracting, you can do the same. For example, to change the date and time above to display in red, the directory in blue, and your username on a yellow background, you could try this:

    [e[36m]w[e[0m]n[[e[1;43m]u[e[0m]@H]$ "

Let’s analyze this code:

  • [..] declares some unprinted characters
  • e[.. клавиша эскейп. То, что следует особая последовательность вывода, чтобы изменить цвет (или другие характеристики) в терминале
  • 31m красный текст ( 41м будет красный фон)
  • 36m является голубым текстом
  • 1;43m объявляет желтый фон ( 1;33m будет желтый текст)
  • [ е[0m] at the end resets the default terminal colors

You can find more colors and tips in the quick Bash HOWTO. You can even make the text flip or blink! Why someone on the planet would want to do this, we do not know. But you can!

What are your favorite bash quick settings? Let us know in the comments.