将 bash 脚本添加到路径 [英] Add a bash script to path
问题描述
我想向 linux PATH 添加一个小脚本,这样我就不必在实际放置在磁盘上的位置实际运行它.
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.
该脚本非常简单,就是通过我制作的代理授予 apt-get 访问权限:
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $@ )
len=${#array[@]}
_args=${array[@]:1:$len}
sudo http_proxy="http://user:password@server:port" apt-get $_args
然后我将它保存为 apt-proxy.sh,将其设置为 +x (chmod),当我在放置此文件的目录中时,一切正常.
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 添加到 PATH 以便我实际上可以将它称为真正的 apt-get ?[从任何地方]
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
寻找仅命令行的解决方案,如果您知道如何通过 GUI 进行操作,那很好,但不是我正在寻找的.
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
推荐答案
试试这个:
- 将脚本保存为
apt-proxy
(没有.sh
扩展名)在某个目录中,比如~/bin
. - 将
~/bin
添加到您的PATH
,输入export PATH=$PATH:~/bin
- 如果您永久需要它,请在
~/.bashrc
中添加最后一行.如果您使用的是zsh
,请将其添加到~/.zshrc
中. - 然后你可以使用你的参数运行
apt-proxy
并且它会在任何地方运行.
- Save the script as
apt-proxy
(without the.sh
extension) in some directory, like~/bin
. - Add
~/bin
to yourPATH
, typingexport PATH=$PATH:~/bin
- If you need it permanently, add that last line in your
~/.bashrc
. If you're usingzsh
, then add it to~/.zshrc
instead. - Then you can just run
apt-proxy
with your arguments and it will run anywhere.
请注意,如果您在特定窗口中export
PATH 变量,它不会在其他 bash 实例中更新.
Note that if you export
the PATH variable in a specific window it won't update in other bash instances.
这篇关于将 bash 脚本添加到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!