ホーム 暮らし [CentOS7] How to change the PATH environment variable: Temporary and permanent

[CentOS7] How to change the PATH environment variable: Temporary and permanent

篠原 沙世 篠原 沙世 • 更新: • 公開: • 読了約 14 分
目次

Hello.

As the title suggests, I would like to write down how to change the PATH environment variable settings,
both temporarily and permanently.
I always forget how to do this...

There is plenty of information out there if you Google it, so it's not a major problem,
but if I can reach a state where I can make a quick decision by looking at my own article,
it should save me some time...


The table of contents for this article is as follows.

1. What does it mean to set a PATH?

n17c85683a161

Many highly skilled people have already provided easy-to-understand explanations on this topic,
so it might be meaningless for a novice like me to add comments now, but I will do so anyway.
In my mind, I have the following image of what setting a PATH means.


figure created by draw.io


You might think it's difficult when you hear the term PATH environment variable, but
I think it's easier to understand if you imagine it as a "dictionary that the OS refers to."

The PATH dictionary is like a table of contents, and it tells you where to look to check the contents of a command.

For example,

cat /etc/hosts

When a user inputs this, the OS that receives the input first recognizes, "I should cat /etc/hosts!"

However, it doesn't know the meaning of cat.
Should it read it? Or should it delete it?

To confirm this, it goes to look at the PATH.
The PATH contains directories that might hold the files describing what kind of processing each command performs.

The OS goes to check those directories one by one,
and if it finds the file, it executes its contents;
if it doesn't find it, it gets angry at the user, saying "There's no such thing!"
That is how I perceive it.



2. How to temporarily change the PATH

To temporarily change the PATH environment variable,

export PATH = $PATH:(追加したいディレクトリ)

By writing it like this, you can set the
PATH, although it will only be temporary.


For example, suppose there is a
file containing command processing details under /usr/pgsql-13/bin/.

[postgres@ssh_server ~]$ ls /usr/pgsql-13/bin/ 
clusterdb   initdb             pg_controldata  pg_receivewal  pg_test_timing   postgres                    reindexdb 
createdb    pg_archivecleanup  pg_ctl          pg_resetwal    pg_upgrade       postgresql-13-check-db-dir  vacuumdb 
createuser  pg_basebackup      pg_dump         pg_restore     pg_verifybackup  postgresql-13-setup 
dropdb      pg_checksums       pg_dumpall      pg_rewind      pg_waldump       postmaster 
dropuser    pg_config          pg_isready      pg_test_fsync  pgbench          psql


When executing any of these commands, you would normally want to execute them
using only the command name rather than the full path.
In other words, I think you would input it like the circle below, but not like the X.
If you specify the full path,
you can execute the command without editing the PATH environment variable.

○ initdb ~~~~~
× /usr/pgsql-13/bin/initdb ~~~~~


If you want to input and execute a command like the circle above,
you need to add the directory to the PATH environment variable.
Checking the default PATH shows it is as follows,
and you can see that /usr/pgsql-13/bin/ is not registered.
Because it is not registered, the OS is complaining, "It's not anywhere!"

[postgres@ssh_server ~]$ echo $PATH 
/sbin:/bin:/usr/sbin:/usr/bin 


Let's register it in the PATH and check again.
You can confirm that it is included.
If you execute the command in this state, the process will complete normally.
This is because the OS will go to check /usr/pgsql-13/bin/.

[postgres@ssh_server ~]$ export PATH=$PATH:/usr/pgsql-13/bin/ 
[postgres@ssh_server ~]$ echo $PATH 
/sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-13/bin/


This way of setting the PATH is fine, but if you set it this way,
the settings will disappear if you, for example, reboot.

I think it is very troublesome to have to register it again,
so I will describe how to change it permanently next.



3. How to change the PATH permanently

You can change the PATH permanently
by modifying the contents of .bash_profile in the user's home directory.

First, confirm that .bash_profile exists in the home directory of the user
for whom you want to set the PATH.
Since it starts with a ".", it is a hidden file.
It will not be output with just the ls command, so check it with ls -a or similar.

[postgres@ssh_server ~]$ pwd 
/home/postgres 
[postgres@ssh_server ~]$ ls -a 
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .psql_history  data 


If you check the contents of this .bash_profile, it should look like the following.
By writing the directory you want to add on the last line of this file,
you can set the PATH permanently.

[postgres@ssh_server ~]$ cat .bash_profile 
.bash_profile 
Get the aliases and functions 
if [ -f ~/.bashrc ]; then 
       . ~/.bashrc 
fi 
User specific environment and startup programs 
PATH=$PATH:$HOME/.local/bin:$HOME/bin 
export PATH 


In this case, since we want to add /usr/pgsql-13/bin/,
we will append it as follows.

[postgres@ssh_server ~]$ cat .bash_profile 
.bash_profile 
Get the aliases and functions 
if [ -f ~/.bashrc ]; then 
       . ~/.bashrc 
fi 
User specific environment and startup programs 
PATH=$PATH:$HOME/.local/bin:$HOME/bin 
export PATH 
export PATH=$PATH:/usr/pgsql-13/bin/ 

同じカテゴリの記事

篠原 沙世

アニメとゲームの新作情報を担当。制作会社・配信元の公式告知と登壇情報を照合して記事化します。

おすすめの関連記事