Linux 将 bash 脚本添加到路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20054538/
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-07 01:22:48  来源:igfitidea点击:

Add a bash script to path

linuxbashshellpath

提问by Mansueli

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.

我想向 linux PATH 添加一个小脚本,这样我就不必在实际放置在磁盘上的位置实际运行它。

The script is quite simple is about giving apt-get access through a proxy I made it like this:

该脚本非常简单,就是通过代理提供 apt-get 访问权限,我是这样制作的:

#!/bin/bash
array=( $@ )
len=${#array[@]}
_args=${array[@]:1:$len}
sudo http_proxy="http://user:password@server:port" apt-get $_args

Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.

然后我将它保存为 apt-proxy.sh,将其设置为 +x (chmod),当我在放置此文件的目录中时,一切正常。

My question is : how to add this apt-proxyto PATHso I can actually call it as if it where the real apt-get ? [from anywhere]

我的问题是:如何将此apt-proxy添加到PATH以便我实际上可以将其称为真正的 apt-get ?[从任何地方]

Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.

寻找仅限命令行的解决方案,如果您知道如何通过 GUI 进行操作,那很好,但不是我正在寻找的

采纳答案by jlhonora

Try this:

尝试这个:

  • Save the script as apt-proxy(without the .shextension) in some directory, like ~/bin.
  • Add ~/binto your PATH, typing export PATH=$PATH:~/bin
  • If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrcinstead.
  • Then you can just run apt-proxywith your arguments and it will run anywhere.
  • 将脚本另存为apt-proxy(不带.sh扩展名)在某个目录中,例如~/bin.
  • 添加~/bin到您的PATH,键入export PATH=$PATH:~/bin
  • 如果您永久需要它,请将最后一行添加到您的~/.bashrc. 如果您正在使用zsh,则将其添加到~/.zshrc
  • 然后你可以运行apt-proxy你的参数,它会在任何地方运行。

Note that if you exportthe PATH variable in a specific window it won't update in other bash instances.

请注意,如果您export在特定窗口中设置 PATH 变量,则它不会在其他 bash 实例中更新。

回答by Brian Agnew

You want to define that directoryto the path variable, not the actual binary e.g.

您想将该目录定义为路径变量,而不是实际的二进制文件,例如

PATH=$MYDIR:$PATH

where MYDIRis defined as the directory containing your binary e.g.

其中MYDIR定义为包含二进制文件的目录,例如

PATH=/Users/username/bin:$PATH

You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.

你应该把它放在你的启动脚本中,例如 .bashrc 以便它在每次调用 shell 进程时运行。

Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you couldname your script as apt-getand put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.

请注意,顺序很重要,并且对 PATH 进行评估,以便如果在路径变量的较早条​​目中找到与您的名字匹配的脚本,那么您将执行该脚本。因此,您可以将脚本命名为 asapt-get并将其放在路径的较早位置。我不会这样做,因为它很混乱。您可能希望改为调查 shell 别名。

I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd tothat directory and run it inadvertently.

我还注意到你说它在你当前的目录中工作正常。如果您的意思是您的路径 ( .) 中有当前目录,那么这就是潜在的安全风险。有人可以将某个通用实用程序(例如ls)的某些木马变体放在一个目录中,然后让您进入cd to该目录并在不经意间运行它。

回答by Abhi

make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.

将可执行文件的别名添加到 ~/.bash_profile 文件中,然后从任何地方使用它,或者您可以获取包含您需要从任何地方运行的可执行文件的目录,这将为您解决问题。

回答by Alfredo Morales Pinzón

As a final step, after following the solution form proposed by @jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:

作为最后一步,在遵循@jlhonora ( https://stackoverflow.com/a/20054809/6311511)提出的解决方案后,更改文件夹“~/bin”中文件的权限。你可以使用这个:

chmod -R 755 ~/bin