# Executing programs without having to specify a directory?

Why is it that I have to type /etc/init.d/apache2 (args) in order to run apache??

Isn't there a way to just apache2 args ??

-

You don't normally type a command to run Apache: it starts when the system boots. Scripts in /etc/init.d start services and are executed automatically at boot time, if your system is set up correctly. With most distributions, services are enabled by default and will start unless you've done something to disable them.

If you've stopped Apache and want to restart it, /etc/init.d/apache2 start is the normal way — or preferably service apache2 start. It would be of little use to have a shorter command since this is not something that you would do as part of normal system use. Commands that are intended for common use are can be executed without specifying their full path; these commands are in directories listed in the PATH environment variable (which Windows imitates).

-

You should probably be using the service command to start it.

service apache2 start

And the apache arguments should be in your /etc/apache2 structure.

-

That is because /etc/init.d/ is not set in $PATH. It's essentially a :-separated list of directories. When you execute a command, the shell searches through each of these directories, one by one, until it finds a directory where the executable exists. ( http://www.cs.purdue.edu/homes/cs348/unix_path.html ) - While this is true, some shells will expand aliases, search for functions, and check it's internal hash prior to searching PATH. – jordanm Nov 16 '12 at 23:18 There are many ways to invoke a command (script or binary executable) that's not within the reach of your PATH. Those methods are good to know - but, if you find yourself pulling all the more fancy stunts just to execute commands, then that would indicate a less than optimal system design. Let's say this is your script, called s, and you place it in /home (to make it unreachable, as /home is most certainly not in your PATH): echo "The first argument is:$1"
echo "The second argument is: $2"  Three ways to still reach it, without giving the absolute path, are: An alias: alias salias='/home/s' A function: sfunc () { /home/s$@ }

A link: ln -s /home/s slink

The first two, if you'd use them again, you'd put in your shell initialization file (.bashrc for bash, .zshrc for zsh, etc.). To use the link (the third case), you'd have to put it in a directory included in you PATH, or, you'd invoke it explicitly, with an absolute path (e.g., if you're in the same directory, ./slink one two).

-