You could store your scripts where they belong in the filesystem, and create a bin
directory in your home. Adding
if [ -d "$HOME/bin" ] ; then
export PATH="$HOME/bin:$PATH"
fi
in your .bashrc
makes any executable placed in ~/bin
discoverable. Finally, you just need to add files in the dorectory. You can use symbolic links to whatever script you want to make discoverable in ~/bin
, which allows you to virtually change the name of the script and leave it where you want it on you filesystem. As an example, for a file my_script.sh
, first make sure the file is executable
chmod u+x my_script.sh
then create a symbolic link
ln -s my_script.sh ~/bin/my_script
in the dedicated folder. Note that the extension was removed for convenience. You can now run your script from anywhere using the command my_script
. You don't have to make the symbolic link every time you edit the original my_script.sh
file.
Edit : to make any text file executable via a certain interpreter, you can use a shebang. For a bash script, this means adding
#!/bin/bash
as a first line for the file. Note that the technique is not restricted to bash scripts, but also applies to python for instance using
#!/usr/bin/env python
Note : I personnally use ~/.local/bin
instead if ~/bin
as a personal preference, but most people use a bin
directory directly located in their home, not hidden. Many distributions integrate it directly, such as debian or ubuntu which automatically add such a directory in the PATH
if it exists (in the default .profile
file they ship). My choice is based on the fact that many softwares already use .local/share
, that I consider it as a configuration tool rather than as a set of real files (only symbolic links), and that I don't want this folder to mess with the completion.
.sh
or.bash
extensions on your scripts is not a best practice. Executable scripts define commands, and UNIX command names don't have extensions. You don't runls.elf
, do you? – Charles Duffy May 06 '15 at 17:47