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.
In your function definition, I would suggest replacing:
echo "cat $1 | pv -w 20 -s ${__size}"
with just:
cat $1 | pv -w 20 -s ${__size}
This way, the function itself will execute this bit of code, without requiring a call to eval
in the caller.
Best Answer
If it's only for your personal use then you could add it to your shell's initialization file as a function, e.g.
~/.bashrc
.For a summary of the different initialization files in Bash you can consult the Bash Guide for Beginners:
Also see the Bash Reference Manual:
A typical pattern would be to put your function definition in your
~/.bashrc
file and source that file from your~/.bash_profile
.But it's probably worth noting that which profile file to use can depend on your OS, your terminal application, and your own preferences. See for example the following posts on AskDifferent:
Why doesn't .bashrc run automatically?
Why doesn't Mac OS X source ~/.bashrc?
Also see this post on StackOverflow:
Alternatively, you can create a personal directory for your own scripts (e.g. I use
~/local/bin
) and then add that directory to your PATH in your profile file (i.e.export PATH="${HOME}/local/bin:${PATH}
).If you want to make it available to other users then you might put it in
/usr/local/bin
as a script (rather than/usr/bin
).For further discussion regarding where to put executable files, see the following posts:
/usr/bin vs /usr/local/bin on Linux
Where should a local executable be placed?