14

Background: One of my colleagues who doesn't come from a Linux background asked me about using ./ before some commands and not others, so I explained to him how PATH works and how binaries are chosen to be run. His response was that it was dumb and he just wanted to not need to type ./ before commands.

Question: Is there a way to easily modify the behavior of the shell such that $PWD is always the first item on PATH?

  • Dumb it may (or may not) be, but every environment has it's quirks, and this is a typical Unix shell quirk. – RonJohn Oct 26 '17 at 13:20
  • 12
    It's a security measure. Shells used to do this by default (think 1970s), and multi-user systems were widely attacked on account of it. – Charles Duffy Oct 26 '17 at 17:31
  • 3
    His response was that it was dumb: said colleague needs to educate his/her own self on this topic. That off-the-cuff answer demonstrates a PHB-style response: no understanding or appreciation of the underlying system. – DevNull Oct 26 '17 at 20:44
  • 2
    Why not just drop the binaries into ~/bin and make it part of PATH ? This keeps things organized ,too. – Sergiy Kolodyazhnyy Oct 26 '17 at 23:04
  • 3
    @Ziazis on Windows, the current directory is considered regardless of whether it’s part of PATH (which is the usual source of such considerations). – Stephen Kitt Oct 27 '17 at 9:58
54

If you really want to, you can do this by prepending . to your path:

export PATH=".:$PATH"

However, that’s a bad idea, because it means your shell will pick any command in the current directory in preference to others. If someone (or some program) drops a malicious ls command in a directory you use frequently, you’re in for trouble...

  • 13
    This is an excellent point, although perhaps it would be sufficient to move the . to the end of the path then export PATH="$PATH:." – Christophe Oct 26 '17 at 9:55
  • 12
    Indeed I remember having . at the beginning of your PATH was the default for normal users (if I created the user with "sam" on HP-UX, at least; I'm not sure about old SunOS). Later this default was removed for security reasons. If you have the . as the last item of your PATH, still someone can hope for typical typos. – Philippos Oct 26 '17 at 10:49
  • 6
    @Philippos, yes like the infamous /tmp/sl prank scripts to target users with such PATHs. On Debian and derivatives, install the sl package to get an idea of how often you type sl in place of ls. – Stéphane Chazelas Oct 26 '17 at 15:55
  • No, it is not a bad idea, it is a good one, especially if you're a developer. Of course, keeping an environment where no one drops in malicious commands is another good idea. – jamesqf Oct 26 '17 at 18:27
  • 4
    @jamesqf, actually, it's a good idea to type out ./ when you want to call commands in the current directory. Or, if your usual current directory is /project/under/development/bin, then add that to your PATH. This solves your developer use case. Putting . at the start of your PATH is a bad idea, period. – Wildcard Oct 26 '17 at 19:58
13

Although this is not a direct answer to your question, best practice would be to create a bin directory in your home directory as /home/user/bin:

mkdir -p "$HOME/bin"
echo 'PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc"
source "$HOME/.bashrc"

Have your personal scripts put in there. The PATH would be traversed in the order you define it and execute your personal scripts without the . needed.

0

You need to have the . in the beginning of your PATH variable:

export PATH=".:$PATH"

Your Answer

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

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