12

This question already has an answer here:

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!

marked as duplicate by jasonwryan, Gilles bash Oct 14 '14 at 23:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The teacher has testscript in their $PATH... – jasonwryan Oct 14 '14 at 22:11
  • can you explain how to add testscript to $PATH? and what exactly do I have to do? – asura Oct 14 '14 at 22:12
  • Put it in a directory in your PATH. – jasonwryan Oct 14 '14 at 22:12
  • You can always add an alias in your bashrc file.. – ryekayo Oct 14 '14 at 22:15
12

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.

  • 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
2

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'

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

  • (as stated in @jimmij's answer, it can be a little insecure) – Yann Hourdel Oct 14 '14 at 22:26
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.