在PATH中加入本地路径(Adding a Directory to the Path)

2023-05-16

Executive Summary
Adding a directory to the path of a user or all users would seem trivial, but in fact it isn't. The best place to add a directory to the path of a single user is to modify that user's .bash_profile file. To add it to all users except user root, add it to /etc/profile. To also add it to the path of user root, add it to root's .bash_profile file. 
Disclaimer
Obviously, you use this document at your own risk. I am not responsible for any damage or injury caused by your use of this document, or caused by errors and/or omissions in this document. If that's not acceptable to you, you may not use this document. By using this document you are accepting this disclaimer.
Pre and Post Pathing

Linux determines the executable search path with the $PATH environment variable. To add directory /data/myscripts to the beginning of the $PATH environment variable, use the following:
PATH=/data/myscripts:$PATH
To add that directory to the end of the path, use the following command:
PATH=$PATH:/data/myscripts
But the preceding are not sufficient because when you set an environment variable inside a script, that change is effective only within the script. There are only two ways around this limitation:
If, within the script, you export the environment variable it is effective within any programs called by the script. Note that it is not effective within the program that called the script.
If the program that calls the script does so by inclusion instead of calling, any environment changes in the script are effective within the calling program. Such inclusion can be done with the dot command or the source command. Examples:
. $HOME/myscript.sh
source $HOME/myscript.sh
Inclusion basically incorporates the "called" script in the "calling" script. It's like a #include in C. So it's effective inside the "calling" script or program. But of course, it's not effective in any programs or scripts called by the calling program. To make it effective all the way down the call chain, you must follow the setting of the environment variable with an export command.

As an example, the bash shell program incorporates the contents of file .bash_profile by inclusion. So putting the following 2 lines in .bash_profile:
PATH=$PATH:/data/myscripts
export PATH
effectively puts those 2 lines of code in the bash program. So within bash the $PATH variable includes $HOME/myscript.sh, and because of the export statement, any programs called by bash have the altered $PATH variable. And because any programs you run from a bash prompt are called by bash, the new path is in force for anything you run from the bash prompt.

The bottom line is that to add a new directory to the path, you must append or prepend the directory to the $PATH environment variable within a script included in the shell, and you must export the $PATH environnment variable. The only remaining question is: In which script do you place those two lines of code? 
Adding to a Single User's Path

To add a directory to the path of a single user, place the lines in that user's .bash_profile file. Typically, .bash_profile already contains changes to the $PATH variable and also contains an export statement, so you can simply add the desired directory to the end or beginning of the existing statement that changes the $PATH variable. However, if .bash_profile doesn't contain the path changing code, simply add the following two lines to the end of the .bash_profile file:
PATH=$PATH:/data/myscripts
export PATH
Adding to All Users' Paths (except root)

You globally set a path in /etc/profile. That setting is global for all users except user root. Typical /etc/profile files extensively modify the $PATH variable, and then export that variable. What that means is you can modify the path by appending or prepending the desired directory(s) in existing statements modifying the path. Or, you can add your own path modification statements anywhere before the existing export statement. In the very unlikely event that there are no path modification or export statements in /etc/profile, you can insert the following 2 lines of code at the bottom of /etc/profile:

PATH=$PATH:/data/myscripts
export PATH
Adding to the Path of User root
User root is a special case, at least on Mandrake systems. Unlike other users, root is not affected by the path settings in /etc/profile. The reason is simple enough. User root's path is set from scratch by its .bash_profile script. In order to add to the path of user root, modify its .bash_profile.

Summary
A fundimental administration task is adding directories to the execution paths of one or more users. The basic code to do so is:
PATH=$PATH:/data/myscripts
export PATH
Place that code, or whatever part of that code isn't already incorporated, in one of the following places:
User            Class Script to modify
One user     $HOME/.bash_profile
All users      except root /etc/profile
root             /root/.bash_profile

 

From:http://www.troubleshooters.com/linux/prepostpath.htm

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在PATH中加入本地路径(Adding a Directory to the Path) 的相关文章

  • AOSP的编译及刷机

    简介 众所周知 xff0c Android是开源的 xff0c AOSP xff08 Android Open Source Project xff09 为Android开源项目的缩写 作为一名Android开发 xff0c 掌握Andro
  • Linux常用命令记录(du、find、grep、hadoop/hdfs、sed、tar、tr)

    Linux常用命令 查询格式 语句1 语句2 语句3 xff1a 对语句1的输出结果进行语句2的判定 xff0c 然后对输出结果进行语句3的判定 如 xff1a cat a txt head 10 wc l 39 cat a txt 39
  • 虚拟机运行出现蓝屏的现象如何解决

    前两天给大家分享了如何在电脑上安装虚拟机 xff0c 听到有部分小朋友私信跟我反馈说 xff0c 自己本身电脑可以安装vm虚拟机但是他安装过后一运行就立马进入蓝屏修复界面 所以今天想跟大家分享一下遇见这种情况如何解决 xff08 本文以华硕
  • 小白也能学懂——子网划分(2)

    我前天讲了一下子网划分 xff0c 昨天比较忙碌就忘记写剩下的内容了 xff0c 今天吃过饭 xff0c 想给他补上 xff0c 主要还是细分一下子网划分的作用 xff0c 以及如果进行计算 xff0c 本章还不是算难 xff0c 但是计算
  • 三分钟告诉你什么是三层交换机!

    昨天上周我们讲了单臂路由和跨交换机传输 xff0c 今天想说一下三层交换机 xff0c 对了还有个小实验 xff0c 收到反馈说我每次都是在图里标注代码不够清晰 xff0c 所以接下来会在实际中把代码贴出来供大家复制使用 目录 一 三层交换
  • 链路聚合(二层链路和三层链路)

    昨天主要介绍了三层交换机 xff0c 今天顺其自然就讲到了链路聚合 xff0c 因为是交换机中一个比较重要的技术 xff0c 下面我们开始 目录 一 单臂路由和三层交换的复习 二 端口绑定技术 三 链路聚合 端口聚合 端口绑定实现的条件 四
  • 静态路由(也许是目前最全的)

    今天在公司 xff0c 新来了个实习生 xff0c 突然问道静态路由的问题 xff0c 他跟我讲他不会设置 然后我就很尴尬 xff0c 因为这个毕竟是基础知识嘛 所以今天整理了一下静态路由的知识 xff0c 跟大家分享一下 目录 一 路由器
  • C# 读取Json文件--代码示例

    1 C 读取Json文件 JsonConvert SerializeObject str object to string JsonConvert DeserializeObject obj string to json 2 Json文件创
  • 网络地址转换协议——NAT(恐怕是最全的版本)

    前天我说第二天要跟大家讲一下NAT的 xff0c 结果放假有些懒 xff0c 所以就放在今天更新 xff0c 希望大家不要凶我 xff0c 哈哈哈 目录 一 什么是NAT 1 NAT简介 2 NAT作用 3 NAT内网地址的范围 4 主要应
  • linux日志文件详解

    目录 一 日志文件的分类二 日志文件位置三 常见日志文件1 分析日志文件2 内核及系统日志 四 日志消息等级五 日志文件分析1 用户日志2 程序日志 六 日志分析注意事项 一 日志文件的分类 日志文件是用于记录Linux系统中各种运行消息的
  • 虚拟化与docker基础

    文章目录 一 虚拟化1 虚拟化概述2 虚拟化的功能3 虚拟化的三种模式4 容器与虚拟化 二 Docker1 容器概述2 Docker概述3 Docker的设计宗旨4 容器与虚拟机的区别5 容器在内核中支持两种重要的技术6 Docker核心概
  • Docker容器网络模式与数据管理

    文章目录 一 Docker容器操作1 容器创建2 查看容器的运行状态3 启动容器4 创建并开启容器5 终止容器运行6 容器的进入7 复制文件到容器中 宿主机中8 容器的导出与导入9 删除容器 二 Docker网络1 Docker网络实现原理
  • docker镜像的创建与dockerfile

    文章目录 一 docker镜像的创建1 创建镜像的方法2 基于现有镜像创建3 基于本地模板创建4 基于dockerfile创建 二 Dockerfile1 概述2 Dockerfile结构3 Dockerfile镜像结构的分层4 Docke
  • matlab中值滤波实现

    中值滤波是一种典型的非线性滤波 xff0c 是基于排序统计理论的一种能够有效抑制噪声的非线性信号处理技术 xff0c 基本思想是用像素点邻域灰度值的中值来代替该像素点的灰度值 xff0c 让周围的像素值接近真实的值从而消除孤立的噪声点 该方
  • 程序员的情人节

    今天是一个好的节日 xff0c 七夕呀 xff01 程序是最好的女朋友 xff0c 它是不会骗你的 它偶尔会发些小的情绪 只是你没有懂它
  • stm32-Hardfault及内存溢出的查找方法

    STM32内存结构 1 要点 1 1 两种存储类型 RAM 和 Flash RAM可读可写 xff0c 在STM32的内存结构上 xff0c RAM地址段分布 0x2000 0000 0x2000 0000 43 RAM size Flas
  • raylib部分源代码功能解读

    官网 https www raylib com https github com raysan5 raylib 我根据自己的需求裁剪了多余功能后的代码 xff1a https gitee com xd15zhn raylib https g
  • 无量纲处理、量纲变换与实时仿真理论

    基本原理 万有引力公式 d 2 r
  • 局域网windows平台下时间同步

    最近单位出现很多应为系统时间不统一造成的问题 xff0c 如 客户机时间与服务器时间不同步 xff0c 而客户机使用软件是读取本机时间上传服务器 xff0c 这样就会造成排序错误 每次开机修改很繁琐 我就想到了在局域网内假设时间服务器的想法
  • 水下潜航器的建模与控制

    线性系统理论大作业 待完成 题目 水下潜器模型 xff0c 可能是潜艇或者鱼雷等对象 一个主推进螺旋桨 xff0c 前后两对水平陀翼 xff0c 后面一对垂直陀翼 潜器前进过程中 xff0c 通过调节助推进螺旋桨推力 xff0c 以及三对陀

随机推荐