LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Question about running programs with ./ (https://www.linuxquestions.org/questions/linux-newbie-8/question-about-running-programs-with-315399/)

bbohl 04-20-2005 02:22 PM

Question about running programs with ./
 
Hello, new to Linux so Pardon My Ignorance.....

I've noticed that some programs aren't "found" when I run them, unless I run them like this:

$ ./myprogram

For example, if I have a program in /usr/programs/myprogram, and I do this:

$ cd /usr/programs
$ myprogram

It gives me an error that it can't find myprogram.
But if I run it like this:

$ cd /usr/programs
$ ./myprogram

It runs. This only seems to happen with some programs. Anyone know why this is?

kencaz 04-20-2005 02:30 PM

It is a script file. ./myprogram

try
cat ./myprogram
to see script contents.

http://www.freeos.com/guides/lsst/

KC

coldsalmon 04-20-2005 02:51 PM

Yes, the way Linux manages executables is much different than in MS-DOS (I'm assuming that this is where you're coming from). In Linux, you can only execute commands that are in certain directories, such as /bin, /sbin, /usr/bin. These directories are contained in the PATH variable. You can see this list by typing:

$ echo $PATH

This makes it very easy to execute commands from wherever you are in the directory tree without having to cd to the directory that contains the executable, or having to type out the whole path name. If you want to execute a command that is not in one of these designated directories, you have to type the absolute path name (eg /usr/program/myprogram or ./myprogram, if you are in /usr/program already). While this might seem like an inconvenience, it prevents you from accidentally executing files in the current directory when you really wanted to execute a generic system command. For example, it prevents someone from creating a trojan named "ls" which would be run whenever you wanted to use the ls command. It also prevents commands from behaving differently depending on which directory you are in.

Hope that helps,

--C

bbohl 04-20-2005 04:37 PM

Thanks so much for the answer! I've been reading about Symbolic Links, which is another new topic that seems related.
For example, when I installed firefox I put it in /usr/firefox. To run the firefox program, rather than adjusting the PATH variable to add /usr/firefox or having to cd /usr/firefox and then run ./firefox, I could create a symbolic link in /usr/bin for firefox?

bigrigdriver 04-20-2005 06:16 PM

You could edit your user .bashrc or .bashrc_profile (different distros have different names for it) to add an alias. Such as: alias firefox="/usr/firefox". Then, everytime you boot up, the command is available to you by simply typing firefox and pressing enter.
To make such an alias available to all users, edit the /etc/bashrc to add the alias there. Be adivsed that, on upgrading your distro, the system bashrc may be overwritten, in which case you could loose all of your aliases. In other words, keep a backup copy, which you can then cut and paste your aliases into a changed bashrc.

IBall 04-20-2005 07:13 PM

You can always change your PATH variable though. For example, if you wanted to have a bin directory in your home, you would add the following line to .bash_profile:
Code:

export PATH=$PATH:/home/iball/bin
If you want to be able to execute programs in the current directory, (NOT generally a good idea, ,particularly for root), add the following line to .bash_profile
Code:

export PATH=$PATH:.
I hope this helps
--Ian


All times are GMT -5. The time now is 09:18 PM.