Linux Bash Shell (6): Setting Environment Parameters

Source: Internet
Author: User

Continued Linux Bash Learning (5): Special files, aliases, options, and parameters

 

This article is also the third chapter of "Learning the bash Shell" 3rd Edition, Customizing Your Environment's Reading Notes II, but we will not limit it to this. Setting up a good user environment is crucial for our development. This section describes four aspects: special files, aliases, options, and parameters. The last step is to learn the parameter settings.Continue ......

Parameter settings

Command to find the path

Run the PATH storage command to find the file. You can use echo $ PATH to view the file. All commands are execution files. If the directory where the command is located is not in the PATH, you must specify the directory where the file is located during execution. PATH can contain multiple file directories separated by ":", for example/Bin:/usr/local/bin:/usr/X386/bin

. They are sequential. we enter a command to search in order until it is found.

Some commands are compiled in Linux, such as cd and echo. These commands are not executed through files. We sometimes need to add these paths. For example, we put some execution files ~ /Bin, we want to directly run it during login without specifying the path. We add at the end of the. bash_profile FilePATH = $ PATH:/home/wei/bin

Note the order here. To avoid interfering with system commands (which may also cause system security risks) by running commands in our own directory, we usually place the new paths at the end. If a conflict occurs, and we want to use the command placed in the following directory, the execution path is required, for example, in ~ /Bin/more, this command conflicts with the system command, use $~ /Bin/more

.

Command hashing

To speed up command query in the PATH, Linux uses the hashing command. When shell finds a command in PATH, it puts the command into the hash table. When you use the command again, the query will be performed in the hash table first. You can useHash

Command to view the current hash table, which includes not only the full path of the command, but also the number of times used in this login. AvailableHash-r

To clear the entire hash table.-D

To delete a command.-P

To add a command (even if the command does not exist ). In the environment, you can use hashall

Set to set on or off, but we do not need to modify it.

Directory query path

When using the cd command, if it is not an absolute path, the query starts at the current path. You can set the CDPATH to specify the query path for the cd command. The format is similar to that of PATH. ":" is used as the split and has a forward and backward order. For example, we setCDPATH = :~ /Mybook

Note that the path cannot start with "/" or start. In the above example, We typed in the cd doc, and now the current directory is searched. If not, then go ~ /Find the doc directory in mybook. Generally, if there is a directory, we can clearly set this directory as a variable, such as mydir = ~ /Myproject/mylearning/, and then enter through cd mydir, instead of using CDPATH.

Other parameters

In addition to the parameters described above, there are many other parameters, which are common below.

Home

: User home directory
Seconds

: The time interval at which shell is invoke.
Bash

: Current bash path
Bash_version

: Shell version. For example, the cygwin version I used is 3.2.49 (22)-release.
Bash_versinfo

: The current main version of the shell. For example, my version is 3.
PWD

: Current path
Oldpwd

: The path before the last cd.

These parameters are used to obtain numeric values. No case is used to set these parameters.

User Environment

Variable

The preceding parameters are actually environment variables, such as HOME, MAIL, PATH, and PWD. If you define a variable as an environment variable, first define it, and thenExport

Var_names

, You can have multiple variable names, which can be combined with the previous values, placed:Export

Var_name

=

Value

.

Sometimes we want a variable value to only play a role in a sub-process, you can useVar_name=

ValueCommand

. Generally, variables can be defined in any part of a command, not just the beginning.

There are some variables that are not build-in, but they are already common and have specific meanings. Includes the following:

  1. Bash: displays the path of the Current Shell, such as/usr/bin/bash.
  2. Columns: Number of columns displayed
  3. Editor: Text Editor path
  4. Lines: number of lines displayed
  5. Shell: shows the path of the shell used for login, such as/bin/bash, which is the same as Bash.
  6. Term: The current teminal type, such as cygwin

Some of these have become our environment variables, some are global variables set in/etc/profile, we can also define our own in. bash_profile.

In Linux, there is a file that specifically records user environment variables ~ /. Bashrc. If you want to use this file, add it to. bash_profile.Source. bashrc

To ensure the effect of these environments during login. We can also. bash_profile, but it is generally recommended to place OPTIONS and alias in. bashrc, and a small number of definitions are placed in. bash_profile. For the definition of a large number of variables. bashrc.

Document Revision

When we modify. bash_profile ,. bashrc can certainly use vi and other editor methods, but we can also use echo to append the required settings to the end of the file. For example, we need to set PS1 = "/u /! -> ", We need to add it to the end of. bash_profile and use

$Echo

'Ps1 = "/u /! --> "'>

~ /. Bash_profile

Note that single quotes are used here to avoid the change in the meaning of $ or "or".> It indicates that the object is appended to the end, and> indicates that the entire object is overwritten.

Related links:
My articles on Linux operations

Related Article

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.