3

To make scripts accessible from anywhere in the terminal, one can use the following sequence of commands:

PATH=$PATH:some_path_here
export PATH

Which includes all files at the path sent in alongside all of its subfolders. Which means from then on just typing the name of a script stored within this folder structure will execute it.

How do I export only a single file this way instead of an entire folder alongside all its subdirectories?

  • 1
    I don't know how you can export a file. But why don't you, for example move your script to a new directory (to have only that script in your dir) and add the directory to the path? – fromnaboo Jun 21 '12 at 8:23
  • It would complicate matters in the folder structure that I have alongside decreasing overview of the system. Also I don't like living with limitations. – Hermann Ingjaldsson Jun 21 '12 at 8:40
  • 2
    Regarding your comments about folder structure: Note that a files in a PATH'd directory are in the PATH, but files in subdirectories (of that directory) are not automatically included in the PATH. Each directory must be individually incluede in PATH. – Peter.O Jun 21 '12 at 10:14
7

Seems alias is what you need. You can specify any word to alias and set what will be execute when you execute your word.

alias newcommand='/path/to/your/command'

You can put it in you ~/.shellrc (where shell is your shell, so for bash it will be ~/.bashrc) and it will be available after start.

For more information look here.

  • The issue with this is that the alias will not be available in other processes, correct? Whereas if it'd been added to the path it would have been. For example, in ruby system("myalias") returns nil. – zachaysan Apr 10 at 16:17
  • alias does not work in subshells from my testing – Alexander Mills May 30 at 17:57
4

Create a symbolic link to your file, and place it in a suitable PATH'd directory, eg $HOME/bin or whatever.

ln -s "/path/to/my-file"  "$HOME/bin/my-file" 
  • I think this is preferable to the alias method (currently the accepted answer) as the alias will not work in a non-interactive shell (e.g. from within a Make file) whereas this solution will. – Marcus Junius Brutus Jan 21 '16 at 10:16
3

To explain the background for Rush's answer, export does not do anything with the executables in $PATH. export PATH simply means "I want the PATH variable to be inherited by all child processes." And what happens when you type myprogram on a prompt and press Enter is a series of lookups within the shell.

When a command is specified in BASH without a pathname (e.g. myprogram, or ls), and it isn't an alias, function, builtin or keyword, BASH searches through the directories in PATH, in order from left to right, to see whether they contain an executable of the name you typed.

$PATH, in other words, is a list of directories, which is why it can't be used for single commands (except implicitly, by putting the executable in a separate directory). You can't promote a command to a builtin or keyword, but you can create a function or alias. See the previous link for a simple explanation of their differences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.