linux + run some script without "./" + alias

Your shell will search the paths listed in the $PATH environment variable for the typed command if it's not fully qualified (e. g. vi instead of /usr/bin/vi). You can easily add another path to your $PATH variable by appending a line in your ~/.profile or ~/.bashrc:

export PATH="$PATH:/path/to/your/scripts"

As kind of best practice you should also save your script under /usr/local/bin or /usr/local/sbin, see hier(7).


You need to have the location of the script in an environment variable called PATH.

Environment variables are like normal variables you would use in your shell, except that they are persistent throughout your logged in session. Furthermore, if you set them to be loaded into your environment whenever you log in, they will always be set and available all the time.

The PATH variable helps the shell find the commands you enter. Its value is a list of directories that the shell searches every time you enter a command. The directory names are separated by colons.

If you are using the bash shell (the most common shell used in Linux), then you can see what directories are in the PATH variable by typing echo $PATH at the command prompt.

Let's just say that the load_info script is in a directory called /var. You can add this additional directory to the PATH variable by typing:

PATH=$PATH:"/var"

To make this a permanent change to the PATH variable, you can add this to three different files depending on what effect you want.

  • If you add it to the /etc/profile file, you will make the directory available to all users who log into your system.

  • You can add the PATH modification to your own .bash_profile file in your home directory, and this will only apply to you when you log in.

  • You could also add the PATH modification to the .bashrc file file in your home directory, which would have a similar effect to adding it to .bash_profile, except that the .bashrc file is loaded every time you launch a new shell.

It is also worth mentioning that /var is probably not the best place for a script anyway. /usr/local/bin would be better. Check out the heir(7) man page with man 7 heir for more information on what files should go where.


There are some ways to do this: (running as root)

  1. Make a symlink to the /usr/bin

    # ln -s /var/load_info /usr/bin/load_info
    
  2. Add it to the global PATH environment variable:

    # echo "export PATH=$PATH:/var" >> /etc/profile