Book Read Free

Learning Old School Linux

Page 12

by Ed Hartnett

There are dozens of built-in fields, such as the date and time field above (which can be formated by putting the correct string between the curly-braces).

  Some useful fields are the hostname (h or H), the user name (u), and the current working directory (w or W). There are many ways to get the date and time, or one or the other, in addition to the 'D{}' shown above.

  Living Color

  Setting the prompt also allows you to change the colors of the text, even the color of the background. It's a bit of black magic, but add the string '[33[1;31m]' at the beginning of your prompt, and the string '[33[0m]' at the end. The first blob of funny characters tells the shell to use light red as the text color, and the other blob tells the shell to turn off any color changes and go back to the default color. If you don't include this part, not only what you type, but also the computer's response, will be in light red.

  Of course, light red is not the only color available—the complete list of color codes can be found in the bash man page. They range from black (0;30) to white (1;37), with many in between, such as cyan (0;36), brown (0;33) and light purple (1;35).

  If you add a 10 to the color number, the background color is changed instead of the text color. For example, including '[33[1;45m]' turns the background color to light purple. The '1;45' is the light purple color number (1;35), with 10 added to the second number.

  I don't know who came up with this crazy way of setting colors in the prompt, because it could hardly be more confusing. But using both features together allows you to change both background and text color. For example, setting your prompt to '[33[1;45m][33[1;37m]d [33[0m]' will give you the date in white, against a purple background.

  In that funny-looking string, there are four parts. The first part, '[33[1;45m]', turns on the purple background. The second part, '[33[1;37m]', turns on the white text. The third part, 'd ', shows the date, followed by a space, and the last part, '[33[0m]', turns off all the fancy color stuff so that the command line is shown with default color and background.

 

‹ Prev