54

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.

Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • 2
    `sudo ... apt-get "${@:2}"` is probably simpler and more robust than trying to build a second array. – chepner Nov 18 '13 at 18:20
  • Depending on your needs/options, either pointing `PATH` to the script (already detailed in answers) or moving the script to a location already available will both achieve the desired result. I usually drop a script in `/usr/local/bin`. – Origineil Nov 18 '13 at 18:39
  • Thanks @chepner, I am new to bash programming, so I appreciate such suggestions to improve my code. – Mansueli Nov 18 '13 at 19:00

5 Answers5

94

Try this:

  • Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
  • Add ~/bin to 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 ~/.zshrc instead.
  • Then you can just run 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.

jlhonora
  • 10,179
  • 10
  • 46
  • 70
  • 4
    You don't need to export `PATH` as it is (very likely) already marked as exported. Also, it might be better to _prepend_ your personal paths, rather than to _append_ them to existing `PATH`. – gniourf_gniourf Nov 18 '13 at 18:38
  • 2
    @gniourf_gniourf why is it better to prepend? – compguy24 Feb 05 '19 at 15:44
  • 12
    @compguy24: so that they are searched before. If you have an executable `my_command` in both `dirA` and in `dirB`, and your `PATH` is `PATH=dirA:dirB` then the one in `dirA` will be found first, and hence it's `dirA/my_command` that will be run. Usually, the personal ones are meant to override the system-wide ones; that's why it's usually preferable to prepend your personal ones to `PATH`. – gniourf_gniourf Feb 05 '19 at 17:19
  • note that zsh might not realize the `~` properly. you may use absolute path or `$HOME` instead. – Ali Ghanavatian Sep 29 '21 at 07:51
11

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

PATH=$MYDIR:$PATH

where MYDIR is defined as the directory containing your binary e.g.

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.

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 (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • You might want to take a look at those two detailled answers on [unix.stackexchange.com](http://unix.stackexchange.com/a/26059/73013) and on [superuser.com](http://superuser.com/a/183980/95837) – Iam Zesh Jun 19 '14 at 11:29
1

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
0

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.

Abhi
  • 51
  • 3
0

adding to @jlhonora your changes in ~./bashrc or ~./zshrc won't reflect until you do source ~./zshrc or source ./bashrc , or restart your pc

IAmAliYousefi
  • 1,132
  • 3
  • 21
  • 33