LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How does linux know it's collection of command line programs? (https://www.linuxquestions.org/questions/linux-newbie-8/how-does-linux-know-its-collection-of-command-line-programs-828042/)

munyung 08-23-2010 02:03 PM

How does linux know it's collection of command line programs?
 
Hey guys, i just started using ubuntu after being a long time windows user.

what i find really interesting is that in command line, i can type many programs and commands, eg firefox can be run via command line from anywhere. In windows cmd prompt, im used to having to run the .exe file by first navigating to it, then being able to run the .exe.

what i wanna know is how does linux know all the programs at the command line?

ddusza 08-23-2010 02:18 PM

The simplest answer is that in Linux (and UNIX as well), there is an environment variable "PATH" which has a list of where many of the programs/scripts you run are located. You can see what your PATH variable is set to by typing the command "echo $PATH" at the command prompt. Here is an example:

ddusza@warlock:~$ echo $PATH
/home/ddusza/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/ddusza/bin

The order is also somewhat important as it means the command will be searched for in the directory locations in order (from the sample PATH above):

/home/ddusza/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/home/ddusza/bin

In the case of firefox from your post, firefox is located in /usr/bin--which is where it gets executed from.

If you try to run something which isn't in your PATH, it will typically come back with a "not found" error message. You can also add and remove directories from your PATH variable on the command line and in your .bashrc/.cshrc/.profile, depending on what shell you are using.

Hope this answers your question.

Don

Disillusionist 08-23-2010 02:24 PM

Just in case you are interested, Windows also has a PATH environment variable.

From the cmd:
Code:

echo %PATH%
In UNIX and Linux the PATH is seperator is a colon symbol
In Windows this cannot be used as it forms part of the location and so a semicolon is used instead

r3sistance 08-23-2010 02:25 PM

Quote:

Originally Posted by munyung (Post 4075329)
what i wanna know is how does linux know all the programs at the command line?

Simply put it doesn't? It's the shell that finds out what commands it has, there are some commands what will be directly run by the shell itself or commands it finds by the environmental variables (ie PATH for Bash), you can do "echo $PATH" to find the currently assigned paths for the shell session, as not all users have the same paths (root has considerably different defaults paths to a normal user).

As far as the OS goes, it's the layer that supplies the middle ground between the hardware and the software... so "Linux" as in the Kernel does not know what's being run, or what it's doing, it just runs it... I guess that's a simplistic way to put it. The shell is what knows where commands are.

munyung 08-23-2010 02:27 PM

awesome, this was the answer iv been looking for for days!

$PATH is something completely new to me, i guess i should be doing some reading on this then.

thanks don!

*edit* whoah fast replies, thanks guys. learned something new about windows too.

ddusza 08-23-2010 02:31 PM

No problem. In case you are really feeling daring, you can also issue the command 'env' at the command prompt, which will list out all of the defined variables in your shell environment.

Don


All times are GMT -5. The time now is 03:16 AM.