Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 45

Linux Bible - Christopher Negus

Скачать книгу

$HOME/.bashrc file:

       $ nano $HOME/.bashrc

      With the file open in nano, move the cursor down to the bottom of the file (using the down arrow key). Type the line you want (for example, you could type alias d='date +%D'). To save the file, press Ctrl+O (the letter O); to quit, press Ctrl+X. The next time you log in or open a new shell, you can use the new alias (in this case, just type d). To have the new information you just added to the file available from the current shell, type the following:

       $ source $HOME/.bashrc $ d 06/29/19

      The following sections provide ideas about items to add to your shell configuration files. In most cases, you add these values to the .bashrc file in your home directory. However, if you administer a system, you may want to set some of these values as defaults for all your Linux system's users.

      Setting your prompt

      Your prompt consists of a set of characters that appear each time the shell is ready to accept a command. The PS1 environment variable sets what the prompt contains and is what you will interact with most of the time. If your shell requires additional input, it uses the values of PS2, PS3, and PS4.

      When your Linux system is installed, often a prompt is set to contain more than just a dollar sign or pound sign. For example, in Fedora or Red Hat Enterprise Linux, your prompt is set to include the following information: your username, your hostname, and the base name of your current working directory. That information is surrounded by brackets and followed by a dollar sign (for regular users) or a pound sign (for the root user). The following is an example of that prompt:

       [chris@myhost bin]$

      If you change directories, the bin name would change to the name of the new directory. Likewise, if you were to log in as a different user or to a different host, that information would change.

Special Character Description
\! This shows the current command history number. This includes all previous commands stored for your username.
\# This shows the command number of the current command. This includes only the commands for the active shell.
\$ This shows the user prompt ($) or root prompt (#), depending on which type of user you are.
\W This shows only the current working directory base name. For example, if the current working directory was /var/spool/mail, this value simply appears as mail.
\[ This precedes a sequence of nonprinting characters. This can be used to add a Terminal control sequence into the prompt for such things as changing colors, adding blink effects, or making characters bold. (Your Terminal determines the exact sequences available.)
\] This follows a sequence of nonprinting characters.
\\ This shows a backslash.
\d This displays the day name, month, and day number of the current date, for example, Sat Jan 23.
\h This shows the hostname of the computer running the shell.
\n This causes a newline to occur.
\nnn This shows the character that relates to the octal number replacing nnn.
\s This displays the current shell name. For the bash shell, the value would be bash.
\t This prints the current time in hours, minutes, and seconds, for example, 10:14:39.
\u This prints your current username.
\w This displays the full path to the current working directory.

      TIP

      If you are setting your prompt temporarily by typing at the shell, you should put the value of PS1 in quotes. For example, you could type export PS1="[\t \w]\$ " to see a prompt that looks like this:

        [20:26:32 /var/spool]$.

      Adding environment variables

      You might want to consider adding a few environment variables to your .bashrc file. These can help make working with the shell more efficient and effective:

       TMOUT This sets how long the shell can be inactive before bash automatically exits. The value is the number of seconds for which the shell has not received input. This can be a nice security feature, in case you leave your desk while you are still logged in to Linux. To prevent being logged off while you are working, you may want to set the value to something like TMOUT=1800 (to allow 30 minutes of idle time). You can use any Terminal session to close the current shell after a set number of seconds, for example, TMOUT=30.

       PATH As described earlier, the PATH variable sets the directories that are searched for the commands that you use. If you often use directories of commands that are not in your path, you can permanently add them. To do this, add a PATH variable to your .bashrc file. For example, to

Скачать книгу