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.

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?

UPDATE

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

# source aliases
ALIASFILE=~/.aliasesrc
source $ALIASFILE
function add_alias() {
    if [[ -z $1 || -z $2 || $# -gt 2 ]]; then
        echo usage:
        echo "\t\$$0 ll 'ls -l'"
    else
        echo "alias $1='$2'" >> $ALIASFILE
        echo "alias ADDED to $ALIASFILE"
    fi
}
share|improve this question

2 Answers 2

up vote 8 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

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

if [ -f ~/.zsh/zshalias ]; then
    source ~/.zsh/zshalias
else
    print "404: ~/.zsh/zshalias not found."
fi
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

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.