Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a way to have bash automatically load shell scripts I create and place into /usr/bin?

Or is there a different place I should put custom shell scripts so that they are automatically loaded?

For example suppose I create a script called myscript with contents:

#!/bin/bash
alias e=echo

and make it executable (chmod a+x), and put it into usr/bin. I am not able to call e from the commandline (when I try, it says the command wasn't found).

What am I doing wrong?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

The alias you have defined will only take effect once the alias command is actually called. Thus if you were to do this:

$ source myscript
$ e "Hello"

It should work. However, this is clearly not ideal since you want e to be available whenever you start bash. Luckily, bash provides a number of ways to automatically run commands when the shell is started. For the full details of how bash starts up, see the manual page. For our purposes, it is enough to know that ~/.bashrc (that is, a file named ".bashrc" in your home directory) is run as your shell starts up. One way to make your alias available at startup would be to add this line at the end of ~/.bashrc:

source myscript

However, if you were to do this for every alias you wanted, your /usr/bin folder would likely become a mess. And, if you are on a multi-user system, filling /usr/bin/ with scripts like this may cause other users problems as well. Thus, it is better to place your aliases right inside .bashrc and forgo the separate script all together. Since you are using Ubuntu, inside your .bashrc file you probably have something that looks like this:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases 
fi

This code looks for a file call .bash_aliases in your home directory and runs anything it finds in that file as well. If you have this, or if you add this code to your .bashrc, you could also put your alias in ~/.bash_aliases. This provides an easy way to keep all your aliases in one place and keep your .bashrc file uncluttered.

share|improve this answer
1  
If you're just trying to define an alias at startup then I recommend the .bash_aliases route. A .bashrc file can get a little long depending on what you do with it so I prefer to have a separate place for alias definitions to go so that you can easily change/add aliases. –  Dason Jan 17 '11 at 18:45

Such commands should be placed in ~/.bashrc

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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