16

It has been bugging me for a very long time and I'm really sick of it. For example there's a script called testscript that compares two directories. In class the prof can just type

testscript dir1 dir2

to get the output, but I have to add ./ before the testscript and hit enter. Then enter dir1 and dir2 next line

How did the prof do that? Is it something with the bashrc thing? I never get how it works. If it's related please explain in plain simple language since I'm new to Linux. Thank you!

jimmij
  • 47,140
asura
  • 309

4 Answers4

13

You need to add directory with your script to the PATH variable:

export PATH="$PATH:/path/to/dir"

or you can even add current directory to the PATH:

export PATH="$PATH:."

The later has some security drawback though.

jimmij
  • 47,140
  • 1
    Note that if $PATH was previously not set (which means search in a default search path), that becomes a very insecure search path: :/path/to/dir, that is search commands in the current directory first. A very uncommon situation, but worth pointing out. – Stéphane Chazelas May 15 '17 at 15:49
3

Another way of doing this is to add an alias in your bashrc file:

vim ~/.bashrc

This is what my bashrc file looks like:

# .bashrc

alias coredb='psql -h 172.x.x.x -U jboss jbossdb'
alias psql='psql -h 172.x.x.x -U rkah portal_db'
alias opendb='psql -h 172.x.x.x -U rkah portal_db'


# Source global definitions

if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
alias menu='bash ./menuScript.sh'
alias vi='vim'

If you look where it says alias that is exactly how you can add it. So for example, you would enter in:

alias testscript='bash ./testscript.sh'

jimmij
  • 47,140
ryekayo
  • 4,763
2

You should put the following line at the end of your .bashrc file:

PATH=$PATH:.    

To do so you can type this command:

echo "PATH=$PATH:." >> ~/.bashrc    

You should then start a new shell to have it work

GAD3R
  • 66,769
0

Another alternative you might consider is to collect those special executable scripts and programs into one directory and put that in the PATH statement. Thus you would not have to use the dreaded '.' (dot) in the PATH variable but still it would do what you wanted.

mdpc
  • 6,834