Bash configuration file/prompt/Change $ path

Source: Internet
Author: User

1. Bash configuration file

In your home directory, run

ls .bash*

You will see these files:

L. bash_history: records the commands you previously entered,

L. bash_logout: the command to be executed when you exit the shell,

L. bash_profile: the command to be executed when you log on to the shell,

L. bashrc: the command to be executed every time a new shell is opened.

 

Note the following two differences :'. bash_profile 'is read only once at the beginning of the session, while '. bashrc 'is read every time a new terminal (such as a new xterm window) is opened. Traditionally, You have to place Defined variables, such as path, in '. bash_profile', and aliases (alias) and functions in '. bashrc '. However, because '. bash_profile' is often set to read '. bashrc' content first, you can put all the configurations into'. bashrc' if it is easy to figure '.

These files are set by each user. System-level settings are stored in files under '/etc/profile', '/etc/bashrc', and'/etc/profile. D. However, you have to use your own configuration file: editing does not require the 'root' permission, but also makes your settings more personalized. When the system level conflicts with the user level settings, user settings are used.

Read the content of '. bashrc'. If you want to save some trouble, put all your configurations into'. bashrc '.

The above files are settings for each user. System-level settings are stored in '/etc/profile', '/etc/bashrc', And the'/etc/profile 'directory. d. You 'd better use your own configuration file: editing does not require the 'root' permission, but also makes your settings more personalized. When system-level and user-level settings conflict, user-level settings are preferred.

Ii. Prompt

Each time you open a console or xterm, you first see the prompt (prompt), which is similar:

account@hostname ~ $

 

By default, the prompt displays your username, Host Name ('localhost' by default), current directory (in UNIX ,'~ 'Indicates your home directory ).

Traditionally, the last character can identify whether you are a common user ($) or 'root '(#).

You can use the $ PS1 variable to set the prompt. Command

echo $PS1

 

The current settings are displayed. The meanings of available characters are described in the 'prompting 'section of man bash.

How can we complete the ideal settings? For forgetful beginners, the default setting is somewhat unfriendly, because the prompt only displays the last part of the current directory. If you see a prompt like this

tom@localhost bin $

 

Your current directory may be '/bin','/usr/bin', '/usr/local/bin', and'/usr/x11r6/bin '. Of course, you can use

PWD (output current directory, print working directory)

Can shell automatically tell you the current directory?

Of course. The settings I will mention here, including the prompt, are mostly included in the file '/etc/bashrc. You can modify the settings by editing '. bash_profile' and '. bashrc' in the home directory.

In man bash, The 'prompting 'section describes these parameters in detail. You can add some gadgets, such as the current time in different formats, command history numbers, and even different colors.

In '~ /. Bashrc ', my favorite settings are:

PS1="/[/033[1m/][/w]/[/033[0m/] "

'Root' in '~ /. The setting in bashrc is:

PS1="/[/033[0;31m/][/w]/[/033[0m/] "

 

The prompt I got is:

[/usr/bin]

 

When 'root' is used, it becomes:

[/usr/bin]

I have removed the Host Name and user name because I don't need them. But first, I can see whether my identity is a common user or a 'root '. Note that the prompt of a common user can be black or white.

 

To get proper color settings on the terminal, you can edit the following script color, grant the execution permission (chmod + x color), and then run the script.

#!/bin/bash

#

#   This file echoes a bunch of color codes to the

#   terminal to demonstrate what's available.  Each

#   line is the color code of one forground color,

#   out of 17 (default + 16 escapes), followed by a

#   test use of that color on all nine background

#   colors (default + 8 escapes).

#

T='gYw'   # The test text

echo -e "/n                 40m     41m     42m     43m/

     44m     45m     46m     47m";

for FGs in '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' /

           '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' /

           '  36m' '1;36m' '  37m' '1;37m';

  do FG=${FGs// /}

  echo -en " $FGs /033[$FG  $T  "

  for BG in 40m 41m 42m 43m 44m 45m 46m 47m;

    do echo -en "$EINS /033[$FG/033[$BG  $T  /033[0m";

  done

  echo;

done

echo

 

A more appropriate setting:

PS1="/u: /w//$ "

In this way, the prompt becomes:

user_name: /usr/bin$

 

You can use the Export command to test different settings (for example, export PS1 = "/u:/W // $ "). If you find a suitable prompt, put the settings in your '. bashrc. This will take effect every time you open the console or terminal window.

You can even set theme for the prompt, that is, match different colors to make it look like a great ol.
C64 prompt. If you are interested in this, take a look.
Bashish (http://hem.passagen.se/arnognulf/index2.html ).

3. Change $ path

Like '$ path' and' $ ps1', '$ path' is also an environment variable. Input

set

 

All currently defined environment variables are listed.

The environment variables you see are defined in the shell configuration file, which may be your own configuration file, it may also be defined by The 'root' through the system-level file under '/etc. If you use X, more variables will be configured by X, your window manager, or the Startup file of the desktop environment.

If you are not clear about these settings, do not change them. It is useful to know how to change the $ PATH variable, because this variable determines which directories The shell will look for commands or programs. If the directory of the command to be executed is in $ path, you do not need to enter the full path of the command. You can simply enter the command. Some third-party software does not place executable files in the standard directory of Linux. Therefore, adding these non-standard installation directories to $ path is a solution. In addition, you will also see how to handle general environment variables.

First, as a convention, all environment variable names are capitalized. Note that Linux is case sensitive. Of course, you can define some variables by yourself, such as '$ path' and' $ path', but shell ignores these variables.

The second point is that the variable name sometimes starts with '$', but sometimes it is not. When setting a variable, you directly use the name without adding '$ ':

PATH=/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin

To get the variable value, add '$' before the variable name ':

echo $PATH

/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin

Otherwise, the variable name will be treated as plain text:

echo PATH

PATH

 

When processing $ PATH variables, note that you must not only replace the variables, but add the new strings to the original values. In most cases, you cannot use 'path =/Some/directory', because this will delete all other directories in $ path, so that when you run the program on this terminal, you have to provide the complete path. Therefore, you can only add:

PATH=$PATH:/some/directory

In this way, path is set to the current value (expressed in $ PATH) + the newly added directory.

 

So far, you have only set the new $ PATH variable for the current terminal. If you open a new terminal and run echo $ path, the old $ PATH value is returned, but the new directory you just added is not visible. Because you previously defined a local environment variable (only available on the current terminal ).

To define a global variable that will take effect in a terminal that will be opened later, you need to output the local variable (export). You can use the 'export' command:

 

export PATH=$PATH:/some/directory

 

Now, if you open a new terminal and enter echo $ path, you can also see the newly set $ path. Note that the 'Port' command can only change the variables in the current terminal and the terminal that will run later. It does not work for running terminals.

To add the directory to your $ PATH permanently, add the line 'Port' to your '. bash_profile' file.

Do not set the path in '. bashrc'; otherwise, the directory in the path may grow unexpectedly. Each time you open a new shell, '. bashrc' will work. Therefore, if you add a directory to the file, each time you open a terminal, the directory will be added. This will cause the PATH variable to keep increasing due to directory replication.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.