This article is the Reading Notes of customizing your environment, chapter 3 of learning the bash shell 3rd edition. Setting up a good user environment is crucial for our development. This section describes four aspects: special files, aliases, options, and parameters.
Special files
There are several special files in the user directory.Ls-
To view these hidden system files .. Bash_profile is only an important file and is often used to set the user environment. This file will be executed when the user logs in. Globally (used by all users):/etc/profile. The new configuration is appended to the end of the file. After the modification, You need to log on again to make the configuration take effect, or use the source command to execute these special files, that isSource. bash_profile
.
Currently,. bash_profile is generally used in Linux. If there is no system,. bash_login will be searched for. profile. There is also the. bashrc file, which is actually executed by the system when subbash is called, that is, We typedBash
To open the new bash environment, which is rarely used in reality .. Bash_logout is called in logout.
Alias
Some aliases are set in the environment file, and can also be used in normal bash command lines. Using aliases, we can replace Linux commands with commands that are easy to remember or frequently used by other operating systems, or replace a long string of commands with simple words. The format is as follows:
Alias
Name
=Command
Note that spaces are not allowed before and after "=. For example, search is used to replace the SEARCH Command grep. alias search = grep. If the command contains spaces, it must be enclosed in quotation marks. For example, alias ls = 'LS-color '. Of course, do not use wildcard characters for name. We can add an alias to the command, which is allowed. Bash disables loop. If it encounters a loop, it stops further translation. For example, the above example LS is used as LS-color, and the two Ls can generate a loop logically, however, Bash does not allow this situation. In this case, it is a programmer's principle to avoid setting loop settings on multiple alias.
Aliases can only be used at the beginning of a command line. For example, if alias mywork = 'CD/home/Wei/project/mywork' is embedded in mywork, you can directly enter the specified command, however, we cannot alias mywork =/home/Wei/project/mywork, and then CD mywork. aliases will not parse commands that do not come first. In this case, you can use the export method.
Alias: for example, the current alias list
AliasName
: For example, the true meaning of name
UnaliasName
: Unbind the alias of this name
Option
We can useSet-oOptionname
To open an environment option.Set + OOptionname
To close it. Note that-Indicates on and + indicates off. This means-is easier to press, while + requires two keys. You can use set-O to view the current environment switch, for example, to prevent the CTRL-D from triggering logout, we can turn on ignoreeof, and set-O ignoreeof. I think there are a lot of such environment-set parameters, but they are rarely used. You can use man set to determine if you need them. In addition, noclobber is commonly used. You cannot rewrite an existing file by> redirection. In bash 2.0, shopt is provided for setting, and unset is also provided. However, I do not think it is necessary to remember this and it is rarely used in practical applications. This is not recorded here.
Parameter settings
Compared with environment settings, parameter settings are more interesting. set or shopt is rarely used, while the user's environment variable settings, for example, if Java is lib or bin, are passed. bash_profile. The parameter setting isVarname
=Value
In general, varname is capitalized. For example, javadir, we can view a varname. The common method is Echo. For example, we have set TT = 'test for it ', you can view it through ECHO $ TT or ECHO "$ TT. If not, empty rows are provided.
Single quotation marks and double quotation marks
Let's take a look at the following example: varname = Alice; echo "$ varname" and echo $ varname both show Alice, while echo '$ varname' shows $ varname, this indicates that the parameters are parsed under double quotation marks, while single quotation marks indicate that the content of a string of characters is not parsed.
Let's look at an example of varname = 'four space here '. If echo "$ varname" is returned, four space here is displayed. If echo $ varname is equivalent to echo four space here, show four space here. Double quotation marks indicate a whole, and appear as a word. If echo "four space here" is the same, there are multiple spaces before here.
In parameter settings, the list of quotation marks and double quotation marks are required.
Built-in Parameters
Linux contains some built-in parameters. We can modify these parameters to change the configuration. Avoid duplicate names in our custom parameter names.
Some parameters related to history are as follows:
- Before the histcmd record list is the nth command, you can view it with Echo.
- Histfilesize indicates the number of historical commands recorded in the file, that is, the maximum number of lines in the file;
- Histsize indicates the maximum number of historical commands recorded in the memory.
- Histignore: indicates that some commands are not recorded. For example, histignore = L *: & indicates that commands starting with L are not recorded, and duplicate commands are not recorded (& indicates repeated commands)
- Histtimeformat: If it is null, no time is displayed. We can set the time format for it, for example, histtimeformat = "% Y/% m/% d % t", note that double quotation marks are used here.
Time Format
Format |
Replaced |
% |
The locale's abbreviated weekday name |
% |
The locale's full weekday name |
% B |
The locale's abbreviated month name |
% B |
The locale's full month name |
% C |
The locale's appropriate date and time representation |
% C |
The century number (the year divided by 100 and truncated to an integer) as a decimal number [00-99] |
% D |
The day of the month as a decimal number [01-31] |
% D |
The date in American format; the same value as % m/% d/% y. |
% E |
The day of the month as a decimal number [1-31]; a single digit is preceded by a space |
% H |
The same as % B |
% H |
The hour (24-hour clock) as a decimal number [00-23] |
% I |
The hour (12-hour clock) as a decimal number [01-12] |
% J |
The day of the year as a decimal number [001-366] |
% M |
The month as a decimal number [01-12] |
% M |
The minute as a decimal number [00-59] |
% N |
A newline character |
% P |
The locale's equivalent of either a.m. or p. m |
% R |
The time in a.m. and p. m. notation; in the POSIX locale this is equivalent to % I: % M: % S % p |
% R |
The time in 24-hour notation (% H: % M) |
% S |
The second as a decimal number [00-61] |
% T |
A tab character |
% T |
The time (% H: % M: % S) |
% U |
The weekday as a decimal number [1-7], with 1 representing Monday |
% U |
The week number of the year (Sunday as the first day of the week) as a decimal number [00-53] |
% V |
The week number of the year (Monday as the first day of the week) A decimal number [01-53]; if the week containing 1 January has four or More days in the new year, then it is considered week 1-otherwise, it Is the last week of the previous year, and the next week is week 1 |
% W |
The weekday as a decimal number [0-6], with 0 representing Sunday |
% W |
The week number of the year (Monday as the first day of the week) A decimal number [00-53]; all days in a new year preceding the first Monday are considered to be in week 0 |
% X |
The locale's appropriate date representation |
% X |
The locale's appropriate time representation |
% Y |
The year without century as a decimal number [00-99] |
% Y |
The year with century as a decimal number |
% Z |
The timezone name or abbreviation, or by nothing if no timezone information exists |
% |
% |
Some Mail-related parameters are also introduced in the book, but in fact we have seldom used mail. Instead, we use e-mail clients such as irebird and 189 mail or web Mail. Therefore, the related parameters are not extracted.
This section describes how to use "/" to see long command segmentation. For example, if the double quotation marks are left quotation marks and no right quotation marks, they must be completed only after the right quotation marks are completed, in the following example, double quotation marks are not required.
$ Echo "Hello,/
> My friend"
Hello, my friend
Prompt
In linux, the prompt is usually $, which is already the root user #. This is related to the specific linux version. We can also set our prompt, such as adding the user name and time. In Linux with four prompts, PS1, PS2, PS3 and PS4. PS1 is the default prompt. For example, we need to change the user name to $. You can reset PS1, that is, PS1 = "/u $". If we want to add time, set to PS1 = "/u-/A $ ".
PS2 is the prompt that the command has not been completely input, and the next line break continues, usually>. For example, we described the segmentation method above. PS3 and PS4 are used for programming and debugging. It will be introduced in subsequent chapters. The following table shows the settings of the prompt.
Command |
Meaning |
/ |
The ASCII bell character (007) |
/ |
The current time in 24-hour HH: MM format |
/D |
The date in "Weekday Month Day" format |
/D {format } |
The format is passed to strftime (3) and the result is inserted Into the prompt string; an empty format results in a locale-specific time Representation; the braces are required |
/E |
The ASCII escape character (033) |
/H |
The hostname |
/H |
The hostname up to the first "." |
/J |
The number of jobs currently managed by the Shell |
/L |
The basename of the shell's terminal device name |
/N |
A carriage return and line feed |
/R |
A carriage return |
/S |
The name of the shell |
/T |
The current time in 12-hour hh: mm: SS format |
/T |
The current time in HH: mm: SS format |
/@ |
The current time in 12-hour a.m./p. m. format |
/U |
The username of the current user |
/V |
The version of bash (E.g ., 2.00) |
/V |
The release of bash ; The version And patchlevel (e.g., 2.00.0) |
/W |
The current working directory |
/W |
The basename of the current working directory |
/# |
The command number of the current command |
/! |
The history number of the current command |
/$ |
If the specified tive UID is 0, print a #, otherwise print $ |
/Nnn |
Character code in octal |
// |
Print a backslash |
/[ |
Begin a sequence of non-printing characters, such as terminal Control sequences |
/] |
End a sequence of non-printing Characters |
In general, we will not modify them unless there are special requirements, so there is no need to remember these system parameter names and check them again when necessary.
To be continued ......
Related links:
My articles on Linux operations