简体   繁体   中英

Setting bash command-line variable to start my application

I'm pretty new to programming for Linux environments, so I don't exactly know what to search for in order to answer this question for myself. I need to understand how applications set the shell to accept a certain command to start them. For example, you can start Firefox from the command line by executing the command: firefox.

I don't know where this is defined. Makefile? Configure script? In the source code itself?

Any resources / reading on Linux programming tidbits like these would be greatly appreciated!

Thank you.

Firefox由命令“firefox”启动,因为$ PATH环境变量中的一个文件夹中有一个名为“firefox”的可执行文件。

Are you talking about the PATH variable? It seems like you are.

In linux, you should be able to type: "echo $PATH" (without quotes) and get a ":"-separated list of locations where programs are located (like firefox).

If you need to add something to your path, you should be able to do:

export PATH=$PATH:/another/directory

In your shell (which is most likely bash)

You can also type:

which firefox

To display the location of the firefox executable.

Typically the shell is going to have an environment variable called $PATH set. This is just an ordered list of all the directories to look when somebody types in a command. As soon as it finds an executable file (by which I mean a file for which you have execute permissions, not a file ending in .exe) with the same name as whatever was typed, it will run that file. Common directories in $PATH might be /bin, /usr/local/bin, ~/bin, etc.

So, when you type 'firefox', the shell looks through all the directories in $PATH until it finds /usr/local/bin/firefox, which it then runs. To make your own programs run the same way, you'll either need to put them (or a symbolic link to them) in a directory that is likely to be in every user's path (/usr/local/bin/ is a good choice), or you'll need to get your users to add your program's directory to their $PATH.

For a more complete description, see the Wikipedia article about the $PATH variable.

As an alternative to the modification of $PATH mentioned earlier, you could also copy or link your executable in one of the directories already in your $PATH. more specifically, /usr/local/bin/ is available on most UNIX system for pretty much this purpose (installing software outside the default package management of the operating system).

It has to be in the path as everyone else mentioned, but you might also need to make it executable with something like this:

chmod +x /path/to/file

And if it's a script there's usually a shebang at the top that tells the os what to use to execute it:

#! /usr/bin/python

Often, large packages are installed in /opt with a wrapper script or link somewhere in the PATH . For example, on my system, Google Picasa is installed in /opt/google/picasa and there is a symlink at /usr/bin/picasa to /opt/google/picasa/3.0/picasa

Firefox is at /usr/bin/firefox on my system and that's a symlink to /usr/bin/firefox-3.0 which is itself a symlink to /usr/lib/firefox-3.0.11/firefox.sh - That shell file fumbles around until it finally runs /usr/lib/firefox-3.0.11/firefox (unless it finds a reason to do something else). That, finally, is a binary executable. So /usr/lib is where firefox is installed, for me.

You can use this command to find out where a program is:

type -a firefox

Then, you can find out what kind of file it is using this:

file /usr/bin/firefox

Also see the Filesystem Heirarchy Standard for more information about recommended locations for files and programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM