Add bash script folder to path?

I have some bash scripts in an application folder that I'd like to use as if they were on my path. If they were straight-up applications, I'd just add the directory to ~/.bashrc, but these are scripts.

eg:

~/a_dir/another_dir/foo.sh
~/a_dir/another_dir/foo-gui.sh

Is there a good way to set up a path so that I can use them from any directory?

eg:

totally/different/path$ bash foo.sh
1

3 Answers

Yes, you can add any directory to the system path. One way to do this is updating the PATH (environmental variable) definition. You can do this in your .bashrc by adding the following lines:

PATH="/your/script/dir:${PATH}"
export PATH

I like to add my scripts to $HOME/.local/bin/ (which is a hidden directory) so my home dir stays cleaner.

Your directory will not get inserted into the PATH variable right away, unless you run source .bashrc.

You can add multiple directories to the path, remember that. Please consult BASH documentation if you do not understand the code.

The previous method will only work for your user. If you need to add a script directory for all users do as bodhi.zazen and add your scripts to /usr/local/bin.

2

IMO the best method is to add the scripts to ~/bin

mkdir ~/bin

~/bin should automatically be added to your path. If not, add this to ~/.bashrc

if [ -d $HOME/bin ]; then PATH=$PATH:$HOME/bin
fi

If you want them to be available for all users, add them to /usr/local/bin

1

another solution

  1. Add path to ~/.bashrc open using vim $ vim ~/.bashrc

    example:

    # add extra paths export PATH=$PATH:~/Scripts

  2. once path is added run:

    $ source ~/.bashrc

  3. If added correctly there should be no errors.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like