[Linux] About PATH

I will describe the terminal command PATH.

What is PATH?

A command to add a command search path. The command search path is a path that searches for the command executable file.

$ ls
/bin/ls

For example, suppose you run $ ls, which references folders and files as above, and you have an executable file with the same name, ls, under the / bin directory. Since $ ls is executing the command without specifying the path, the shell will find the executable file corresponding to the command from each directory.

This path to find is called the command search path, and you can add this path with the PATH command.

How to check the command search PATH

You can check it with $ echo $ PATH. In a certain environment, it will be displayed as follows.

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin 

The paths are separated by : The six / usr / local / bin, / usr / bin, / bin, / usr / sbin, / sbin, and/ usr / local / sbinare the search command path. It is set.

How to check the storage location of the command execution file

You can check it with $ which ls. The execution result is as follows.

$ which ls

#Execution result
/bin/ls

When an executable file with the same name exists in multiple search command paths

In this case, it has priority and is executed from the one on the left output by $ echo $ PATH. For example, in the following cases

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin

It is executed from / usr / local / bin. The priority is / usr / local / bin / usr / bin/ bin / usr / sbin/ sbin/ usr / local / sbin.

Add PATH

To add PATH, write it in the .bashrc or .bash_profile file in the ʻexport PATH = $ PATH: command search path` format you want to add.

Actually add PATH and try to pass

.bashrc


export PATH=$PATH:Command search path you want to add

.bash_profile


export PATH=$PATH:Command search path you want to add

The file that describes the PATH can be either .bashrc or .bash_profile.

$ source ~/.bashrc
$ source ~/.bash_profile

If you do not execute each described file with the source command, the path will not pass.

What is the export command?

Display and set environment variables.

Display environment variables

export -p

#Output result(Only a part is described)
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
declare -x LANG="ja_JP.UTF-8"
declare -x SHELL="/bin/bash"

The set environment variables are displayed.

Setting environment variables

For example, to set the environment variable $ ULB, set as follows.

$ export ULB=/usr/local/bin

$ echo $ULB
/usr/local/bin #Output result

I have confirmed that it is set with $ echo $ ULB.

ls /usr/local/bin
ls $ULB

Both commands produce similar results.

Override environment variables

$ echo $ULB
/usr/local/bin #Output result

$ export ULB=/u/l/b

$ echo $ULB
/u/l/b #Output result

You can also overwrite environment variables as described above.

Delete environment variables

It can be deleted with the ʻunset` command.

$ unset ULB #[$]Does not have to be entered.

$ echo $ULB
#Nothing is output.

Increase the priority of the command search path to be added

$ export PATH=Command search path you want to add:$PATH

The command search path has the highest priority from the left, so describe it like this.

Reference URL

Introduction to Linux ~ What is "passing through" ~ https://qiita.com/Naggi-Goishi/items/2c49ea50602ea80bf015

Recommended Posts

[Linux] About PATH
Linux (about directory path)
About Linux
About Linux
About Linux
About Linux
About Linux ①
[Linux] About export
Linux (about groups)
About Linux kernel parameters
[Linux] Pass through PATH
Basic knowledge about Linux
Linux PATH environment variable
About Linux environment construction (CentOS)
Linux (about files and directories)
Linux (About adding / removing users)
About LINUX files and processes
About Japanese path of pyminizip
Linux
What I learned about Linux
About Linux environment construction (VMware VirtualBOX)
About Linux commands Super basic edition
Beginners have learned about Unix (Linux).
About LangID
About CAGR
linux memorandum
Linux command # 4
About virtiofs
Linux commands
About python-apt
Linux commands
About Permission
About sklearn.preprocessing.Imputer
About gunicorn
Linux command # 3
Linux overview
About locale
Linux basics
About permissions
direnv (linux)
[Linux] [C / C ++] Notes about Waf build system
About Opencv ②
Organize Linux
About axis = 0, axis = 1
linux commands
Linux practice
Ubuntu Linux 20.04
About import
Linux Summary
Linux process
Linux permissions
About numpy
Linux command # 5
About pip
Linux basics
About numpy.newaxis
Forgot linux
About endian
About import
Linux commands
Linux commands