Mansueli
Mansueli

Reputation: 7004

Add a bash script to 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.

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.

Upvotes: 59

Views: 170353

Answers (5)

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

Upvotes: 0

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

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272377

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.

Upvotes: 11

jlhonora
jlhonora

Reputation: 10709

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.

Upvotes: 102

Abhi
Abhi

Reputation: 61

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.

Upvotes: 0

Related Questions