Results 1 to 2 of 2

Thread: Running commands and the PATH environment variable

  1. #1
    Member Newbie
    Join Date
    Oct 2006

    Default Running commands and the PATH environment variable

    When you first start using the command line it's important to understand how the shell locates commands. In Linux and other *NIX systems the shell uses environment variables to hold information that should be available any program running within a certain context (such as within a user's shell). The PATH variable is used to tell the shell which directories it should search in when looking for a command. The variable holds a list of directories, each separated by a :, as can be seen when using the echo command to view the variables value
    -bash-3.00# echo $PATH
    The PATH above represents one typical of the root user. An ordinary user typically won't have directories like /sbin or /usr/sbin in their path. When you type a command at the prompt (like the echo command above), the shell will look through each of the directories listed in PATH for a file named "echo". The first such file it finds it will try to execute as a program. This can be important if you have multiple copies of a program in different directories. If for instance you install a copy of bash in /usr/local/bin/, then you'll want to make sure that the directory /usr/local/bin is listed before /bin in the PATH variable in order to be able to use your installed version by simply typing "bash".

    It is however possible to run commands whose files are in a directory listed in PATH. You can do this by specifying a full path to the file the command is in. The path specified can use either a relative or absolute path.

    A relative path means that the path to the file starts in your current working directory. This is most commonly used to run a command whose file is in your current directory. The relative path will typically start with "./", to let the shell know you're specifying a path to the command. A common example would be to run the configure command when compiling a program from source. This command is included with the source code, so to run the command for a program you're compiling you would use the cd command to move to the directory holding the program's source code, and enter
    Using an absolute path means that you start the path to the command by specifying the path to the command from the root directory. On Linux systems the root directory is specified by the / character, which is used as the path delimiter. So to run a program named mytop in the /home/andrew/bin directory, I could use
    Using an absolute path to the command allows you to run a command no matter which directory the file is located in.

  2. #2
    Junior Member Newbie
    Join Date
    Aug 2007

    Default Commands in the current directory - "dot-slash"

    If you're in the directory where an executable program or script is located, you can't run it just by typing the command name - you'll get this:
    [root@server ~]#
    -bash: command not found
    Why can't the system find an application that's right under its nose? Because the current directory (aliased with a single dot .) isn't part of the PATH environment variable. This is by design, strange as it may seem. It's a safety measure to make sure that applications are run only from the locations they're supposed to be run from.

    For example, let's say you want to start up an ssh session to another server. If you type in 'ssh', the system will execute /usr/bin/ssh (because /usr/bin is part of your PATH). If your current directory (/home/user) was in the PATH, however, someone could plant a trojaned version of the ssh application into your home directory - enabling them to steal your password or hijack your connection into the other server.

    To run a command in the current directory, you need to use "dot-slash" notation:
    [root@server ~]# ./
    This tells the system to look in your current directory for the specified program.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts