19

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
  • you won't be able to do it with bash foo.sh, but with foo.sh, after giving exec permission and putting them in the $PATH.
    – elias
    Jan 23, 2012 at 20:15

3 Answers 3

24

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
  • 2
    Adding it before overrides the system scripts - meaning it could be easy to replace em which ain't always good.
    – Wilf
    Nov 20, 2015 at 16:03
  • 1
    Yeah, the potential of a process dropping say a script called less into your ~/.local/bin directory means a process could override your less command to do their own thing using your permissions and then pass the command to less. Silently usurping your authority on the system. True, other things have to break at that point, but someone could get tricked into that.
    – flickerfly
    Dec 17, 2020 at 19:27
10

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
  • In this case, since it's scripts from an applications, I'd advise to put symbolic links for the scripts in the ~/bin, for better maintainability.
    – elias
    Jan 23, 2012 at 20:13
5

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .