What is the path command in linux

What is the path command in linux

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user. It increases both the convenience and the safety of such operating systems and is widely considered to be the single most important environmental variable.

Environmental variables are a class of variables (i.e., items whose values can be changed) that tell the shell how to behave as the user works at the command line (i.e., in a text-only mode) or with shell scripts (i.e., short programs written in a shell programming language). A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems; its primary function is to read commands that are typed in at the command line and then execute (i.e., run) them.

PATH (which is written with all upper case letters) should not be confused with the term path (lower case letters). The latter is a file’s or directory’s address on a filesystem (i.e., the hierarchy of directories and files that is used to organize information stored on a computer). A relative path is an address relative to the current directory (i.e., the directory in which a user is currently working). An absolute path (also called a full path) is an address relative to the root directory (i.e., the directory at the very top of the filesystem and which contains all other directories and files).

A user’s PATH consists of a series of colon-separated absolute paths that are stored in plain text files. Whenever a user types in a command at the command line that is not built into the shell or that does not include its absolute path and then presses the Enter key, the shell searches through those directories, which constitute the user’s search path, until it finds an executable file with that name.

The concentrating by default of most executable files in just a few directories rather than spread all over the filesystem and the use of the PATH variable to find them eliminates the need for users to remember which directories they are in and to type their absolute path names. That is, any such program can be run by merely typing its name, such as ls instead of /bin/ls and head instead of /usr/bin/head, regardless of where the user is currently working on the filesystem. This also greatly reduces the possibility of damage to data or even to the system as a whole from the accidental running of a script that has the same name as a standard command. 1

A list of all the current environmental variables and their values for the current user, including all the directories in the PATH variable, can be seen by running the env command without any options or arguments (i.e., input data), i.e.,

As there can be considerable output, it can be convenient to modify this command so that it displays just the PATH environmental variable and its value. This can be accomplished by using a pipe (represented by the vertical bar character) to transfer the output of env to the grep filter and use PATH as an argument to grep, i.e.,

Another way to view the contents of just PATH alone is by using the echo command with $PATH as an argument:

echo repeats on the display screen whatever follows it on the command line. The dollar sign immediately preceding PATH tells echo to repeat the value of the variable PATH rather than its name.

Читайте также:  Группы для windows phone

Each user on a system can have a different PATH variable. When an operating system is installed, one default PATH variable is created for the root (i.e., administrative) account and another default is created that will be applied to all ordinary user accounts as they are added to the system. The PATH variable for the root user contains more directories than for ordinary users because it includes directories, such as /sbin and /usr/sbin, that contain programs that are normally used only by that user.

PATH variables can be changed relatively easily. They can be changed just for the current login session, or they can be changed permanently (i.e., so that the changes will persist through future sessions).

It is a simple matter to add a directory to a user’s PATH variable (and thereby add it to the user’s default search path). It can be accomplished for the current session by using the following command, in which directory is the full path of the directory to be entered:

For example, to add the directory /usr/sbin, the following would be used:

An alternative is to employ the export command, which is used to change aspects of the environment. Thus, the above absolute path could be added with the following two commands in sequence

or its single-line equivalent

That the directory has been added can be easily confirmed by again using the echo command with $PATH as its argument.

An addition to a user’s PATH variable can be made permanent by adding it to that user’s .bash_profile file. .bash_profile is a hidden file in each user’s home directory that defines any specific environmental variables and startup programs for that user. A hidden file is a file whose name begins with a dot (i.e., a period) and which is normally not visible; however, it can be seen by using the ls (i.e., list) command with its -a (i.e., all) option.

Thus, for example, to add a directory named /usr/test to a user’s PATH variable, it should be appended with a text editor to the line that begins with PATH so that the line reads something like PATH=$PATH:$HOME/bin:/usr/test. It is important that each absolute path be directly (i.e., with no intervening spaces) preceded by a colon.

It is sometimes desired to run a script or program which has been installed in a user’s home directory or some other location that is not in the user’s default search path. Such script or program can, of course, be run by typing in its absolute path. But an often more convenient alternative when the script or program is in the current directory is to merely precede the command name with a dot slash (i.e., a dot followed by a forward slash and with no intervening spaces). The dot is used in paths to represent the current directory and the slash is used as a directory separator and to separate directory names from file names.

MS-DOS also uses a PATH variable. However, it differs from Unix-like operating systems in that it searches the user’s current directory before it searches in any directories in that variable.

________
1 An extreme example would be the situation in which an ordinary user created a shell script such as rm -r /, which would delete all files and directories in the system for which the user had writing permission, and named this script ls. Were the system administrator to navigate to the directory in which this script was located and attempt to run the standard ls command in order to view the contents of that directory, the shell would instead run the script with the same name and thereby remove the contents of all currently mounted partitions on the computer!

Created June 2, 2006. Updated July 8, 2007.
Copyright © 2006 — 2007 The Linux Information Project. All Rights Reserved.

Читайте также:  Часто стал появляться синий экран смерти windows 10

Источник

How to get full path name of a Linux command

I want to find out the file path of commands in Linux e.g., ls has file path as /bin/ls . How can I find out the exact path of some commands?

5 Answers 5

As pointed out, which

would do it. You could also try:

This will list all the paths that contains progName . I.e whereis -b gcc on my machine returns:

you can use which , it give you path of command:

you can use type :

You can use the which command. In case of a command in your $PATH it will show you the full path:

And it will also show details about aliases:

Yes you can find it with which command

You did not specify, which shell you are going to use, but I strongly recommend using which , as it does not necessarily do, what you expect. Here two examples, where the result possibly is not what you expect:

(1) Example with bash, and the command echo :

would output /usr/bin/echo , but if you use the echo command in your bash script, /usr/bin/echo is not executed. Instead, the builtin command echo is executed, which is similar, but not identical in behaviour.

(2) Example with zsh, and the command which :

would output the message which: shell built-in command (which is correct, but certainly not a file path, as you requested), while

would output the file path /usr/bin/which , but (as in the bash example) this is not what’s getting executed when you just type which .

There are cases, when you know for sure (because you know your application), that which will produce the right result, but beware that as soon as builtin-commands, aliases and shell functions are involved, you need first to decide how you want to handle those cases, and then choose the appropriate tools depending on the kind of shell which you are using.

Источник

What is this $PATH in Linux and how to modify it

I have a few questions on this $PATH in Linux.

I know it tells the shell which directories to search for executable files, so:

  1. What does it mean an environmental variable?
  2. How to change its path? and is it recommended to change it?
  3. IF i change it what are the consequences?

3 Answers 3

To get your path current $PATH variable type in:

It tells your shell where to look for binaries.

Yes, you can change it — for example add to the $PATH folder with your custom scripts.

So: if your scripts are in /usr/local/myscripts to execute them you will have to type in a full path to the script: /usr/local/myscripts/myscript.sh After changing your $PATH variable you can just type in myscript.sh to execute script.

Here is an example of $PATH from RHEL:

To change your $PATH you have to either edit

/.bash_profile ) for user or global $PATH setting in /etc/profile .

One of the consequences of having inaccurate $PATH variables is that shell will not be able to find and execute programs without a full $PATH .

Firstly, you are correct in your statement of what $PATH does. If you were to break it somehow (as per your third point), you will have to manually type in /usr/bin/xyz if you want to run a program in /usr/bin from the terminal. Depending on how individual programs work, this might break some programs that invoke other ones, as they will expect to just be able to run ls or something.

So if you were to play around with $PATH, I would suggest saving it somewhere first. Use the command line instruction

to save it in someRandomFile.txt

You can change $PATH using the export command. So

Читайте также:  Лучший ocr для mac os

HOWEVER, this will completely replace $PATH with someNewPath. Since items in path are separated by a «:», you can add items to it (best not to remove, see above) by executing

The fact that it is an environmental variable means that programs can find out its value, ie it is something that is set about the environment that the program is running in. Other environmental variables include things like the current directory and the address of the current proxy.

Источник

What does $PATH mean?

I am trying to install something and among the steps there was this one:

«Place it on your $PATH «

What does this mean? What is that?

I have searched both this site and on Google but everyone just takes it for granted!

3 Answers 3

Run in a terminal:

what you see is a list of directories, looking like:

If you put an executable in either one of these directories, you do not need to set the path to the executable / script, but you can run it by its name as a command.

Executables in $PATH should not have a language extension by convention (although they would work)

Editing your $PATH variable

You can (permanently) add a directory to $PATH by adding the following line to your

/.profile file (invisible by default, press Ctrl + H in the file manager to make it visible):

More usefull information on environment variables

(such as $PATH ) can be found here (thanks for the suggestions @Letizia)

When one types a command to run, the system looks for it in the directories specified by PATH in the order specified.

You can view the directories specified by typing echo $PATH in the terminal.

Suppose there is a executable file foobar01.sh present at /home/user/foo1/foo2/foobar01.sh which you want to execute on a regular basis. typing the entire «path» would be time consuming. So we add the directory in to $PATH variable and we can execute foobar.sh directly without even specifying the path.

You can add it to $PATH by typing the following command:

I assume you are coming from a Windows background (apologies if it is not true). In layman’s terms, a path (or the search path) is the list of directories that will be searched for anything that you type on the command line. If you type in a built-in command like ls, it will look for a specified list of directories. You can look up your path by typing echo $PATH. Here is one difference between Windows and *nix: By default, Windows always looks for the executable file in the current directory. For example, if you have a file called uptime.bat in c:\myscripts, and you cd c:\myscripts and type in uptime, it will run. However, in *nix, the path will be consulted and the executable found (if available).

If you keep your scripts in a directory called /home/teresa/scripts, to execute those scripts, you will have to specify the full path to that directory. Example: /hone/teresa/checkHost. A variation would be to cd /home/teresa and then type ./checkHost (note the ./ which means that you are explicitly asking the file to run from the current directory.

To avoid this, you can just type

which means that, in addition to the path that already exists now, also search in /hone/teresa/scripts. However, the problem with this is that once you logout, this setting would be gone. So, you should edit the hidden file

/.bashrc, find the PATH line there, and append it accordingly. I am assuming you use bash. In the case of other shells, the syntax and file are different.

As a new user, it is very tempting to have . in the search path, which basically means that also search in the current directory. However, that is not considered a good practice for reasons discussed elsewhere.

Источник

Оцените статью