Add to path only sh file not a dir
The best way is to add a symbolic link to this file in the /usr/local/bin
directory:
cd /usr/local/bin
ln -s server-stop.sh /path/to/your/folder/server-stop.sh
In this case, you are adding a link to the original file, so you can always change the original file and you command will always be working.
Creating links in /usr/bin
works, but unless you want the scripts to be available for all users, I would prefer to create the links in ~/bin
, with the same result, but not touching the global system. Furthermore, OP has not mentioned where the scripts are stored. creating a link globally while the scripts are stored locally is bad practice, so alltogether I would suggest:
- Create a local directory
~/bin
. - Remove the language extension from the scripts (unless they invoke each other), make them executable (see further below).
- Create links to the scripts from
~/bin
(ln -sf). - Log out and back in.
Either with or without language extension, the script would work perfectly well. However, there are some arguments to use the script without language extension:
- For reasons of clarity, it might be preferable to name the link to the script similar to the script itself. keeping the language extension would then be less convenient when invoking the script.
- Although a bit overdone in cases of user-written scripts, according to lintian conventions, scripts (or links to scripts) in default searchpaths should not have a language extension. -
I would use the bash
built-in command hash
to remember the location of the server-stop.sh
script:
hash -p /path/to/folder/server-stop.sh server-stop
Just add the above line to your .bashrc
file.
You can now use server-stop
everywhere in your bash
shell/scripts.
See help hash
:
hash: hash [-lr] [-p pathname] [-dt] [name ...]
Remember or display program locations.
Determine and remember the full pathname of each command NAME. If
no arguments are given, information about remembered commands is displayed.
Options:
-d forget the remembered location of each NAME
-l display in a format that may be reused as input
-p pathname use PATHNAME is the full pathname of NAME
-r forget all remembered locations
-t print the remembered location of each NAME, preceding
each location with the corresponding NAME if multiple
NAMEs are given
Arguments:
NAME Each NAME is searched for in $PATH and added to the list
of remembered commands.
Perhaps an alias is what you are looking for. Open your ~/.bash_aliases
file and add the following to the end of the file(the file may be empty depending upon whether you have added an alias previously):
alias server-stop.sh='/path/to/your/server-stop.sh`
Pros:
This will only add
server-stop.sh
as you intend toThis won't change your
$PATH
variableOther users of your system are unaffected
Cons:
You will have to change the alias once you change the path where
server-stop.sh
is located(I don't think that will be required very frequently)This will work only for bash shell, you will have to see what other shells use for alias.