Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 939
Mansueli Avatar asked Nov 18 '13 18:11

Mansueli


People also ask

How do you set a PATH in a bash script?

For Bash, you simply need to add the line from above, export PATH=$PATH:/place/with/the/file, to the appropriate file that will be read when your shell launches. There are a few different places where you could conceivably set the variable name: potentially in a file called ~/. bash_profile, ~/. bashrc, or ~/.

How do I add something to my PATH?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.


2 Answers

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.

like image 103
jlhonora Avatar answered Oct 20 '22 03:10

jlhonora


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.

like image 41
Brian Agnew Avatar answered Oct 20 '22 04:10

Brian Agnew