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.
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
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.
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]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for .
Try this:
apt-proxy
(without the .sh
extension) in some directory, like ~/bin
.~/bin
to your PATH
, typing export PATH=$PATH:~/bin
~/.bashrc
. If you're using zsh
, then add it to ~/.zshrc
instead.apt-proxy
with your arguments and it will run anywhere. Note that if you export
the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary eg
PATH=$MYDIR:$PATH
where MYDIR
is defined as the directory containing your binary eg
PATH=/Users/username/bin:$PATH
You should put this in your startup script eg .bashrc such that it runs each time a shell process is invoked.
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 could name your script as apt-get
and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
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 (eg ls
) in a directory, then get you to cd to
that directory and run it inadvertently.
将可执行文件的别名添加到 ~/.bash_profile 文件中,然后从任何地方使用它,或者您可以获取包含您需要从任何地方运行的可执行文件的目录,这将为您解决问题。
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:
chmod -R 755 ~/bin
adding to @jlhonora your changes in ~./bashrc or ~./zshrc wont reflect until you do source ~./zshrc or source./bashrc, or restart your pc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.