Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to have a file eg. f with only zsh aliases (pureness reasons). Then I'd like to include f file in my .zshrc file, so that the aliases defined in f are visible in .zshrc.

Is it possible?

If it is, I could create a script eg. my_alias ($my_alias ll 'ls -l') which appends alias to f file. Of course I could do

$echo {alias command} >> ~/.zshrc

but this makes .zshrc one big mess.

Additionally how is it looks like in bash?


If someone share my idea this is solution, thanks to phunehehe:

# source aliases
function add_alias() {
    if [[ -z $1 || -z $2 || $# -gt 2 ]]; then
        echo usage:
        echo "\t\$$0 ll 'ls -l'"
        echo "alias $1='$2'" >> $ALIASFILE
        echo "alias ADDED to $ALIASFILE"
share|improve this question
up vote 12 down vote accepted

.zshrc and .bashrc are script files, not config files, so you "source" the alias file. In Zsh (.zshrc) and Bash (.bashrc) alike:

. my_alias

will run my_alias and leave its effects in the same environment with the RC files, effectively giving you the aliases in the shell. Of course, your are not limited to aliases either. I use a .shrc that is sourced by both .bashrc and .zshrc for common exports, functions and aliases.

For more on sourcing see Different ways to execute a shell script.

share|improve this answer
+1 for .shrc file sourced by both .bashrc and .zshrc – user12345 Apr 6 at 21:32

This is, how I do it in my .zshrc:

if [ -f ~/.zsh/zshalias ]; then
    source ~/.zsh/zshalias
    print "404: ~/.zsh/zshalias not found."
share|improve this answer
Thanks for good answer, You and phunehehe are correct but it's only possible to approve one answer so I decided to give it to phunehehe becasue he was first :) – xliiv Oct 14 '12 at 16:04

To source a file if it exists in one line:

[ -f .aliases ] && source .aliases
share|improve this answer
How is this any different to pat's answer? – jasonwryan Mar 18 '15 at 3:03
Same thing but one-liner. I came from google and the one liner was what I was looking for (found it on a forum at the same time) so I thought I might as well share that ;) I wouldn't have been able to come up with it as I'm dumb in bash – Matthieu Napoli Mar 18 '15 at 9:03

Your Answer


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.