1

In some cases, i need to use ./executable instead of simply executable when running a file in bash. What exactly is the difference and how do i know which one i should be using?

For example, i have an executable adb in DEV/ADT/sdk/platform-tools folder.

In bash, if i enter adb and run it, i get the error : The program 'adb' is currently not installed. You can install it by typing: apt-get install android-tools-adb

But if i type ./adb, it works perfectly.

2 Answers 2

1

The first key point is that ./ specifies the current or working directory, that is, the directory that you are in now which is also the one that you see if you type the command pwd (print working directory). Bearing that in mind, bash works two ways:

  • If you specify a bare executable, such adb where no directory is given, then bash searches along the directories listed in the system PATH for an executable of that name and executes the first such executable that it finds.

    To see what directories are in the system PATH, run

    echo $PATH
    
  • If you specify an executable with a directory, such as ./ in your case, then bash looks only for an executable in that directory. If it is not there, it stops with an error.

In your case, you were in the DEV/ADT/sdk/platform-tools folder where the executable, adb, was located. So, when you type ./adb, bash looks for adb in the current directory and, if it finds it, it executes it.

Alternatively, when you type adb, bash looks through the directories in the PATH. Since DEV/ADT/sdk/platform-tools is apparently not in the PATH, it doesn't find the executable and complains.

Often, people avoid this problem by putting the current directory in their PATH via something like:

PATH="$PATH:."

After this is done, when you type the bare executable's name, such as adb, bash will look for it, as before, along the PATH. But since the PATH now includes the current directory, it will look there also.

The addition of . to the PATH is commonly done for personal users. Because it is more dangerous when root accidentally runs the wrong executable, this is usually not done for root.

1

The reason is that if you don't begin it with ./, it will search the PATH for executable - you can see your current PATH by running echo $PATH - example output:

/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/wilf/.local/bin:/usr/games

So if you want to add DEV/ADT/sdk/platform-tools to your PATH, add the following in ~/.bash_profile (you may need to run source ~/.bash_profile first, or logout and login):

PATH=$PATH:DEV/ADT/sdk/platform-tools

Another example for ~/.local/bin:

PATH=$PATH:$HOME/.local/bin

If you prefix it with ./ , it will search the current working directory (you can find this with echo $PWD, pwd, etc) for the file.

You must log in to answer this question.

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