I have a file called x.sh that I want to execute. If I run:
x.sh
then I get:
x.sh: command not found
If I run:
./x.sh
then it runs correctly. Why do I have to type in ./ first?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because the current directory is not into the
PATHenvironment variable by default, and executables without a path qualification are searched only inside the directory specified byPATH. You can change this behavior by adding.to the end ofPATH, but it’s not common practice, you’ll just get used to this UNIXism.The idea behind this is that, if executables were searched first inside the current directory, a malicious user could put inside his home directory an executable named e.g.
lsorgrepor some other commonly used command, tricking the administrator to use it, maybe with superuser powers. On the other hand, this problem is not much felt if you put.at the end ofPATH, since in that case the system directories are searched first.But: our malicious user could still create his dangerous scripts named as common typos of often used commands, e.g.
slforls(protip: bind it to Steam Locomotive and you won’t be tricked anyway:D).So you see that it’s still better to be safe that, if you type an executable name without a path qualification, you are sure you’re running something from system directories (and thus supposedly safe).