• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

PATH, Command

By Tony Lawrence | on May 13, 2005 | 0 Comment
Linux Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

PATH, Command

Your PATH (echo $PATH) is where your shell will search for executables. If a command you want to run isn’t included in $PATH, you have to be specific: /somewhere/someprog. One confusion for Unix/Linux newcomers is that if PATH doesn’t include “.” (current directory), then trying to run a program you can see right in front of your nose with “ls” is going to generate a “command not found” message from your shell. Unlike MSDOS, Unix shells don’t usually assume they should look in the current directory without being told to. Remember, this is a function of the shell you are using and NOT Unix/Linux itself. Someone COULD write a shell that automatically looked in the current directory for commands – but they aren’t likely to.

Why not? Because it’s a bad idea. It’s especially a bad idea for root because of the very real possibility of being tricked into executing the wrong program with (potentially) disastrous consequences. It’s true that your shell searches through PATH in order, so appending the “.” to the end is less dangerous than sticking it elsewhere, but simple fat-fingered typing can catch you even with that. Especially for root, it’s best not to do that and just learm to type “./this” when you really do want to rn the “this” in the current directory.

It’s definitely a bad idea for root, and probably not a real good idea for anyone. But it does raise a question in my mind – why would you want “.” in your PATH anyway? Yes, good old Microsoft stuff defaults to that behaviour, but that shouldn’t justify defaulting non-root users to include that.. and does any system still do that? I’m not sure I’ve seen one recently.. oops, yes I have: SCO systems still set this by default (at least as of OpenServer 5.0.7) :

PATH=$PATH:$HOME/bin:.

At least it’s at the end..

but other than that, I don’t think I’ve seen “.” added by default anywhere. So what does determine your PATH? A default PATH is probably compiled into your shell, but it’s usually reset or at least added to by startup files. In the case of bash, that would be /etc/profile, /etc/bashrc and your own “dot” files. PATH should be set in .bashrc (but see ./bashrc doesn’t run automatically, .bashrc broken and Shell Bashing) if you are changing or adding to it.

The first place PATH is set is by login. As I mentioned, your shell probably has a default compiled in also; it looks like this on Bash 3.0:

#ifndef DEFAULT_PATH_VALUE

#define DEFAULT_PATH_VALUE \
"/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:."
#endif

Also defined is a failsafe PATH which you can get with “command -p” :

* The value for PATH when invoking `command -p'.  This is only used when

the Posix.2 confstr () function, or CS_PATH define are not present. */
#ifndef STANDARD_UTILS_PATH
#define STANDARD_UTILS_PATH \
"/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc"
#endif

To see how that works, create a little script that just has “echo $PATH” in it. I called mine “p”:

$ cat p

echo $PATH
$ ./p
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pcunixapl/bin
$ bash ./p
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pcunixapl/bin
$ command ./p
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pcunixapl/bin
$ command -p ./p
/bin:/usr/bin
$

Notice that isn’t what the defaults say it would be, so something set that elsewhere during the compile. “command” is useful even without -p because it does ignore aliases and functions, giving more assurance that you are running the command you think you are.

Share this story:
  • tweet

Author Description

No Responses to “PATH, Command”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 495Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    June 2013
    M T W T F S S
    « Oct    
     12
    3456789
    10111213141516
    17181920212223
    24252627282930

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.