Take control of the command line

Setting Default Permissions

As you probably know, permissions define how a file or directory can be used. When you create a new file, it is automatically given a default set of permissions called the umask.

One way to summarize permissions is with three digits. From left to right, they are the permissions for the user account from which the file was created, for those in the same group as the creator, and for all others logged in. Apparently for no other reason than that the idea seemed good at the time, each digit is a sum of numbers in base 8 defining a permission. Read permission is 4, write permission 2, execute permission 1, and no permissions 0. This shorthand is called absolute mode, symbolic permissions, or octal permissions, depending on your preference.

Under this system, a file that everyone could read, write, and execute would have a permission of 777. By contrast, a file that the owner could read, write, and execute, and that nobody else could use at all, would have a permission of 700.

When you create a file and the system assigns the default set of permissions, the umask is defined in /etc/login.defs. However, you can set the umask for each account by adding the umask command followed by the permissions (e.g., umask 700) to .bashrc.

Customizing the Prompt

The command prompt is the recurring text that appears to the left of where you start typing in a command. If you're a new user, you might not have paid much attention to the prompt.

Nor do you absolutely need to these days. In the days before desktops became the norm, the prompt once held useful information that you might want at a glance, such as the current directory, date, and time. Nowadays, when you can get much of this information from the desktop, you might not care so much about the prompt.

Still, even today, you might find it handy to have basic information always visible, such as the current account and directory, even if you are almost always using a virtual terminal. For one thing, you never know when you will need to repair your system when you have no desktop available. Moreover, if you look at many distributions, you often find that the final character in the prompt tells you whether you are using a user or root account; user account prompts end in a dollar sign, whereas root account prompts end in a hash sign (#).

In fact, once you look, you might notice that different distributions have different prompts on the basis of what information they think users might want. For example, if I were working in the /usr/share directory on a computer called nanday, in Fedora, my default prompt would be [bruce@conure share] $, whereas in Debian, it would be bruce@nanday:/usr/share$. From this difference, you might correctly infer that Fedora's defaults assume that users stay largely in their home directories because the full path is not given, whereas Debian's defaults assume that their more advanced users might be anywhere in the system and prefer not to stop to use the pwd command to find out where they are.

In addition, you might want to shorten the prompt, especially if you are listing current paths but tend to have directories nested five or six deep. Alternatively, you might simply want to change the color of the prompt to make it more visible, to please your sense of aesthetics, or to provide a more striking distinction between the root and everyday accounts.

Whatever your reason, the best place to start if you want to change your prompt is by temporarily changing the PS1 prompt parameter. These changes will remain in effect until you either change them yet again or close your current virtual terminal. The next terminal you open will revert to the default prompt.

To begin, you want to view the current settings for PS1, the variable for the prompt, by entering the command echo $PS1. Likely, you will get a response that reads something like: [\u@\h \W]$, which is the reading I get for a machine with Fedora 10 installed. By comparing it with the Fedora prompt mentioned above, you can figure out what each entry in it means. Notice, though, the extra elements, such as the @ between the username (\u) and hostname (\h) and the space between the hostname and the current directory (\W).

You can change this prompt temporarily by referring to Table 1. I have not bothered with some options that are unlikely to be of interest to modern non-developers. If you are curious, you can easily find a complete list online with a quick search.

For instance, if I wanted just to display the current user account, I could make a temporary change by running PS1="\u $ " to produce the prompt bruce $ . Notice that the space after the dollar sign is not an error but was added deliberately so that my typing is separate from the prompt.

If I want to add color, I can change the color of the characters according to the formula:

"\e[x;ym\e[m "

In this formula, \e[ marks the start of the characters that the color applies to, \e[m marks the end, and x;ym is the color code (Table 2). The command to change the prompt to dark red would be:

PS1="\e[0;31m[\u@\h \W]\$ \e[m "

If you substitute a 1 for the 0, you get a lighter version of the same color.

Experiment with these temporary changes until you have the prompt you want. If you make a mistake, you can either use the arrow keys to find an entry in the command history from which you can reverse the mistake or simply close the window if you are working in a virtual terminal.

When you figure out the prompt formula that you want, open the .bashrc file for your account in a text editor and replace the existing formula with the one that you have devised. If you prefer, you can also use the export command: for example, export PS1="\e[0;31m[\u@\h \W]\$ \e[m ".

History Options

The Bash history is a list of previously used commands stored in .bash_history. If you continually type the same or similar commands, you can use the Up and Down arrow keys to scroll to the command you want to use.

The export command sets shell variables and is one way to customize the Bash history. Or, edit .bashrc directly. To set the number of commands stored in the history to 1,200, either enter on the command line or add to .bashrc:

export HISTFILESIZE=1200

Because .bash_history is a text file, it does not require much memory, so you have no reason not to use this setting to increase the default size of 500 entries. Of course, the larger the file, the more scrolling you have to do to find an older command, but you can always open .bash_history in a text editor and use its search function.

Another customization is to keep duplicate commands from being added to .bash_history. For this option, enter:

export HISTCONTROL=erasedups

This customization ensures that .bash_history contains only unique commands, but it might mean that the command you want requires extra scrolling, especially if you have just used a command that already appears well down the list.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Bash Builtins

    Even beginners can benefit from a greater understanding of the Bash shell’s many builtin commands.

  • Introducing Bash

    Beyond all the splash screens, screen savers, and vivid rock-star wallpaper is the simple yet powerful Bash shell.

  • Bash Tricks

    The Bash shell is powerful and infinitely expressive. In this article we describe some tricky techniques you can use to enhance and customize your Bash environment.

  • Command Line: Bash Prompt

    Color-coding your prompt may help avoid configuration errors and data loss. We’ll show you how to design your own custom shell prompt with color and control sequences.

  • Command Line

    A few basic tricks can liven up the command line and add a dash of color to your console.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News