Bash – How to add a bash script to your PATH variable using symlinks

bashcommandpathshellsymlink

I run a gaming server called PocketMine. So basically I have a folder in my home directory that has a bash script to run the server: ~/PocketMine/start.sh

Everytime I want to run the server I either cd into the folder and ./start.sh or PocketMine/./start.sh

I want to know how do I add a symlink called pocketmine in /usr/local/sbin (a lot of progs with symlinks goes here) that will run start.sh and use the contents of its dir.

/usr/local/sbin/pocketmine = ~/PocketMine/./start.sh

Call me lazy but im tired of cd-ing into the folder and running the script instead of just typing one command.

Best Answer

You actually don't need any symlinks, just edit your ~/.bashrc and add the following statement:

PATH=$PATH:$HOME/PocketMine

This avoids polluting your filesystem with unnecessary clutter like symlinks. If you are a csh/tcsh user rather than a bash user, then edit ~/.cshrc and add

set path = ( $path ~/PocketMine )

Personally I'd go one step further in organization. I'd create a ~/bin directory, and put your start script in there, perhaps with a more distinctive name like pmstart (it's not significant that it's a shell script, is it? Maybe someday you want to re-implement it in Python or something. .sh suffices on executables are usually a bad idea because you're exposing and hard-coding an implementation detail (the implementation language) that end-users don't care about, and in the process committing yourself to that implementation detail unnecessarily).

It's likely your $PATH already includes $HOME/bin, but if not you can add it similarly.

Finally, there's are historical conventions/best practices as to what things go into sbin directories and what things go into bin directories. sbin is usually reserved for tools for administrators, while bin is for general end-user utilities.

Related Question