如何在Bash Shell Linux中设置路径?

Linux bash shell provides a lot of information into running applications. PATH is one of the most important ones which is used to locate binary files and libraries. In some situations, we may need to edit, add or remove some paths and locations from the bash PATH variable. In this tutorial, we will differently use cases about these operations.

Linux bash shell为运行的应用程序提供了大量信息。 PATH是最重要的PATH之一,用于查找二进制文件和库。 在某些情况下,我们可能需要编辑,添加或删除bash PATH变量中的某些路径和位置。 在本教程中,我们将对这些操作使用不同的用例。

打印当前PATH变量 (Print Current PATH Variable)

We will start printing the current PATH variable. This will print currently available paths in the PATH variable.

我们将开始打印当前的PATH变量。 这将在PATH变量中打印当前可用的路径。

$ echo $PATH
Print Current PATH Variable
Print Current PATH Variable
打印当前PATH变量

PATH变量语法(PATH Variable Syntax)

As we can see in previous example paths are stored in a single line and delimited with : . Each entry is a separate path to search binaries and libraries.

正如我们在前面的示例中看到的那样,路径存储在一行中,并以:分隔。 每个条目都是搜索二进制文件和库的单独路径。

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bi

添加新路径 (Add New Path)

Now we need to add a new path to the PATH variable. We will put a delimiter which is : and then put the new path we want to add. In this example, we will add /home/ismail/bin as new path.

现在,我们需要向PATH变量添加新路径。 我们将放置一个定界符: ,然后放置我们要添加的新路径。 在此示例中,我们将添加/home/ismail/bin作为新路径。

$ PATH=$PATH:/home/ismail/bin
Add New Path
Add New Path
添加新路径

删除现有路径(Remove Existing Path)

In order to remove the existing path, we should copy the PATH variable value and then remove the path we want to remove. Then set the new PATH variable.

为了删除现有路径,我们应该复制PATH变量值,然后删除要删除的路径。 然后设置新的PATH变量。

导出PATH变量 (Export PATH Variable)

Newly created PATH variable will be available for the current shell sessions. If we need to make this available for all other sessions we should export PATH variable to all other sessions with  export command like below.

新创建的PATH变量将可用于当前的Shell会话。 如果我们需要将此设置用于所有其他会话,则应使用如下所示的export命令将PATH变量导出到所有其他会话。

$ export PATH

使PATH变量永久存在 (Make PATH Variable Persistent)

Even we export our PATH variable this will not make our variable persistent after a reboot all newly added paths will be removed. In order to make the PATH variable persistent, we should add it to .bashrc file which will read before a shell start for the current user.

即使我们导出PATH变量,重启后所有新添加的路径也将被删除,这也不会使变量持久化。 为了使PATH变量具有持久性,我们应该将其添加到.bashrc文件中,该文件将在shell启动之前为当前用户读取。

LEARN MORE  Python Standard Library
了解更多Python标准库
Make PATH Variable Persistent
Make PATH Variable Persistent
使PATH变量永久存在

使PATH变量永久且对所有用户可用(Make PATH Variable Persistent and Available For All Users)

In the previous example, the PATH variable will be available for only the current user. If we need to make it available for all other system users we should change the system-wide file /etc/profile with a text editor. In order to change profilefile, we need root privileges.

在前面的示例中,PATH变量仅对当前用户可用。 如果需要使它可用于所有其他系统用户,则应使用文本编辑器更改系统范围的文件/etc/profile 。 为了更改profile文件,我们需要root特权。

Make PATH Variable Persistent and Available For All Users
Make PATH Variable Persistent and Available For All Users
使PATH变量永久且对所有用户可用

翻译自: https://www.poftut.com/set-path-bash-shell-linux/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值