Linux 如何修改我的用户配置文件以将我创建的脚本文件夹附加到 PATH 变量的末尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9368099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:42:10  来源:igfitidea点击:

How do I modify my user PROFILE file to append a scripts folder i created to the end of my PATH variable?

linuxunixsolaris

提问by user1221987

How do I modify my user PROFILE file to append a scripts folder i created to the end of my PATH variable?
I am not totally sure what this means. Can anyone explain.
Thanks :)

如何修改我的用户配置文件以将我创建的脚本文件夹附加到 PATH 变量的末尾?
我不完全确定这意味着什么。谁能解释一下。
谢谢 :)

回答by blake

The PATH variable stores the list of directories the shell searches for programs/commands when you try to run them. You can access its value from the command line by typing:

PATH 变量存储了 shell 在您尝试运行它们时搜索程序/命令的目录列表。您可以通过键入以下内容从命令行访问其值:

echo $PATH

Be careful when modifying it, otherwise you could interfere with your ability to run programs from the command line. To add a new directory without modifying the original value, you could put a line in your file such as:

修改它时要小心,否则可能会干扰从命令行运行程序的能力。要在不修改原始值的情况下添加新目录,您可以在文件中添加一行,例如:

PATH=$PATH:/directory_to_add

where 'directory_to_add' is the directory you want to add to the path ($PATH tells the shell to insert the value of PATH). Then, if you type the name of one of the scripts in the folder at the command line, it will run without having to type the full pathname (as long as it has execute permission).

其中 'directory_to_add' 是您要添加到路径的目录($PATH 告诉 shell 插入 PATH 的值)。然后,如果您在命令行中键入文件夹中某个脚本的名称,它将运行而无需键入完整路径名(只要它具有执行权限)。

Note - your profile file can be found at ~/.profile, and you can add the line above with a text editor and resave the file. Then, from your home directory, type sh ./.profile, and your path should now include the desired directory.

注意 - 您的配置文件可以在 ~/.profile 中找到,您可以使用文本编辑器添加上面的行并重新保存文件。然后,从您的主目录中,键入 sh ./.profile,您的路径现在应该包括所需的目录。

回答by gleerman

Built-in programs like catand cdsimply work by entering the command. However, they are located in a certain folder, such as /usr/bin/. Try for yourself, and see which folder catis located in, by entering which cat.

内置程序喜欢cat并且cd只需输入命令即可工作。但是,它们位于某个文件夹中,例如/usr/bin/. 自己尝试一下,然后cat输入which cat.

When you type in such command, your shell needs a list of folders in which it has to look for the command just entered. It used the $PATHvariable for this, which stores this list. You can see it by entering echo $PATH.

当您输入这样的命令时,您的 shell 需要一个文件夹列表,它必须在其中查找刚刚输入的命令。它$PATH为此使用了存储此列表的变量。输入即可查看echo $PATH

Now, if you close your shell, the $PATHvariable is gone. When you reopen your shell, it starts a certain amount of scripts, one of them being the .profilescript. In this script, the $PATHvariable is loaded. Therefore, you could adjust the .profilefile in order to save your $PATHpermanently. To do so, simply edit this file and edit the line where $PATHis defined (e.g. pico ~/.profile).

现在,如果您关闭 shell,$PATH变量就会消失。当您重新打开 shell 时,它会启动一定数量的脚本,其中之一就是.profile脚本。在这个脚本中,$PATH变量被加载。因此,您可以调整.profile文件以$PATH永久保存您的文件。为此,只需编辑此文件并编辑$PATH定义的行(例如pico ~/.profile)。

In your particular case, adding your scripts folder to the $PATHlike this, will make you can simply write the name of your script instead of the whole pad when you want to launch one.

在您的特定情况下,将您的脚本文件夹添加到$PATH这样的位置,将使您可以在要启动脚本时简单地编写脚本的名称而不是整个垫。

回答by Art Swri

In unix/linux systems, you have a user id ('john') and a home directory ('/home/john'). The home directory has an abbreviation, the tilde: ~ (at the start of a directory path) means the same as your home directory ("/home/john").

在 unix/linux 系统中,您有一个用户 ID ('john') 和一个主目录 ('/home/john')。主目录有一个缩写,波浪号:~(在目录路径的开头)表示与您的主目录(“/home/john”)相同。

In the home directory are several files that begin with a period (aka dot files because they start with a dot, i.e., a period). When you log in, the shell (i.e., the program that processes the command line when you type commands) that is started to supply you a command line looks for these files and reads them, using their content to initialize your shell environment. You can see these files (if they exist) by entering these commands at the command line:

在主目录中有几个以句点开头的文件(又名点文件,因为它们以点开头,即句点)。当您登录时,开始为您提供命令行的 shell(即在您键入命令时处理命令行的程序)会查找这些文件并读取它们,使用它们的内容来初始化您的 shell 环境。您可以通过在命令行输入以下命令来查看这些文件(如果存在):

cd
ls -a

The cdwith no args means 'change the current directory to be my HOME directory. The lscommand lists files in a directory (among other things); the -aoption says 'show hidden files'. Hidden files are those that start with a period - this is the convention used in unix/linux to 'hide' files.

cd与无参数的意思是“改变当前目录是我的主目录。该ls命令列出目录中的文件(除其他外);该-a选项显示“显示隐藏文件”。隐藏文件是以句点开头的文件 - 这是 unix/linux 中用于“隐藏”文件的约定。

The .profile (said out loud it's often pronounced 'dot profile') file is one such dot file used for initializing your environment.

.profile(大声说出来,它通常发音为“dot profile”)文件就是这样一种用于初始化环境的点文件。

The PATH environment variable is used by the shell to search for executable files (programs).

shell 使用 PATH 环境变量来搜索可执行文件(程序)。

You can google for 'how to update PATH in profile' and similar to learn more about the topic.

您可以在 Google 上搜索“如何在配置文件中更新 PATH”和类似内容以了解有关该主题的更多信息。

Here is a typical snippet found in a .profile file; its purpose is to allow you to run programs that are stored in the directory /usr/mypackage/bin.

这是在 .profile 文件中找到的典型片段;它的目的是允许您运行存储在目录 /usr/mypackage/bin 中的程序。

PATH="/usr/mypackage/bin:$PATH"
export PATH

Putting a directory on the PATH allows you to type just a program name ('myprogram') in place of the longer form ('/usr/mypackage/bin/myprogram').

将目录放在 PATH 上允许您只键入程序名称 ('myprogram') 代替较长的形式 ('/usr/mypackage/bin/myprogram')。

You can see the effect of this snippet using echo $PATH; it will show the entire value of the PATH variable. The value should be a list of paths (directories) separated by colon. A simple example:

您可以使用以下代码查看此代码段的效果echo $PATH;它将显示 PATH 变量的整个值。该值应该是由冒号分隔的路径(目录)列表。一个简单的例子:

echo $PATH
/usr/mypackage/bin:/usr/bin:/bin

That should give you a foothold to begin investigating the details. Trying searching for topics like 'how do I set up my linux/unix login', 'what is .profile file', etc., to learn more.

这应该给你一个立足点开始调查细节。尝试搜索诸如“如何设置我的 linux/unix 登录”、“什么是 .profile 文件”等主题以了解更多信息。

It's advisable to use double-quotes when setting the value of PATH to encapsulate any 'usual' characters that may be in the names of the items in the path. Single quotes are not suitable for this as they will prevent the evaluation of $PATH (which is what supplies your existing path when defining your new path value). For more on quotes, here is one discussion of single vs double quotes

建议在设置 PATH 的值时使用双引号来封装路径中项目名称中可能出现的任何“常见”字符。单引号不适合于此,因为它们会阻止对 $PATH 的评估(这是在定义新路径值时提供现有路径的内容)。有关引号的更多信息,这里是单引号与双引号的讨论