普通用户的bin目录与环境变量

最近一个朋友问我,如何在linux普通用户下,不用切换到服务的执行目录便能够直接调用对应的命令。

这简单来说,就是一个宣告环境变量的问题,于是我参照以前的常用做法,给他把该服务的bin目录宣告(export)到了环境变量(PATH)中,但是后来一想,始终觉得不太妥当,这要是各种服务的bin目录宣告多了,会不会就可能存在同名文件的冲突?

(注:大部分情况下,一个操作系统内不会运行太多的服务,而且软件命令也都会有意无意的规避此类风险,所以宣告PATH环境变量后,导致bin目录文件名冲突的概率其实相当低;但求稳的话,哪怕只有微不足道的一丝风险,能规避还是尽量规避的为好)

于是想了一番,梳理出了一个自认为较为规范的流程。

思路如下:

1. 创建和一个当前目录下的bin目录

2. 将创建的bin目录宣告为环境变量

3. 将需要全局调用的命令软连接到新创建的bin目录

santiagod@santiagod-virtual-machine:~$ mkdir ~/bin   #创建当前用户的bin目录
santiagod@santiagod-virtual-machine:~$ pwd
/home/santiagod
santiagod@santiagod-virtual-machine:~$ echo $HOME
/home/santiagod
santiagod@santiagod-virtual-machine:~$ export PATH=$PATH:$HOME/bin   #宣告bin目录到环境变量
santiagod@santiagod-virtual-machine:~$ echo 'echo test OK' > test.sh
santiagod@santiagod-virtual-machine:~$ ls
bin  Desktop  Documents  Downloads  Music  Pictures  Public  PycharmProjects  snap  Templates  test.sh  Videos
santiagod@santiagod-virtual-machine:~$ mkdir test
santiagod@santiagod-virtual-machine:~$ cd test/
santiagod@santiagod-virtual-machine:~/test$ echo 'echo test OK' > mytest.sh    #创建测试脚本
santiagod@santiagod-virtual-machine:~/test$ ls
mytest.sh
santiagod@santiagod-virtual-machine:~/test$ chmod +x mytest.sh 
santiagod@santiagod-virtual-machine:~/test$ ./mytest.sh
test OK
santiagod@santiagod-virtual-machine:~/test$ mytest.sh
mytest.sh: command not found #链接到bin目录前
santiagod@santiagod-virtual-machine:~/test$ echo $PWD
/home/santiagod/test
santiagod@santiagod-virtual-machine:~/test$ ln -s $PWD/mytest.sh $HOME/bin
santiagod@santiagod-virtual-machine:~/test$ mytest.sh
test OK #链接到bin目录后
santiagod@santiagod-virtual-machine:~/test$

 最后给bin目录添加永久生效

如果自定义的目录正好与我一样就叫bin,那么就可以直接生效了,其他目录则,需要在profile或bashrc文件中宣告一下

ubuntu22.04的普通用户bin目录默认在~/.profile中

santiagod@santiagod-virtual-machine:~$ cat .profile 
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists   #下面自动宣告了bin目录,
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
santiagod@santiagod-virtual-machine:~$ 
View Code

centos8.2的默认普通用户bin目录在.bashrc中宣告

[tom@haproxy1 ~]$ ls -a
.  ..  .bash_logout  .bash_profile  .bashrc
[tom@haproxy1 ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
[tom@haproxy1 ~]$ cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs
[tom@haproxy1 ~]$ 
View Code

centos7.6的默认普通用户bin目录在.bash_profile中宣告

[tom@test234 ~]$ ls .
./             ../            .bash_logout   .bash_profile  .bashrc        
[tom@test234 ~]$ vi .bash_profile
[tom@test234 ~]$ 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
[tom@test234 ~]$ 
View Code

 

posted @ 2022-06-23 00:11  咿呀哒喏  阅读(1072)  评论(0)    收藏  举报