Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a .bashrc file, which I want to setup so it reads aliases from an .aliases file and sets them up.

Currently I have:

# User specific aliases and functions
while read alias_line
do
        echo `alias ${alias_line}`
done < .aliases

But upon login I get:

-bash: alias: -fu: not found -bash: alias: $USER": not found

-bash: alias: -lart": not found

The .aliases file is like this:

psu="ps -fu $USER" ll="ls -lart"
pico='nano'

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

When you use alias ${alias_line}, the variable is broken up at spaces, ignoring quoting rules.

There are two ways you could fix it:

  • Remove all quoting from the alias file:

    ll=ls -lart
    psu=ps -fu $USER
    

    and put the variable itself in quotes:

    alias "$alias_line"
    

    This works because in bash, ll="ls -lart" and "ll=ls -lart" are exactly equivalent.

  • Alternatively (this is a better and more common way), have a file with alias commands, and use the . builtin (aka source) to import it.

    alias pico='nano'
    alias psu='ps x'
    alias ll='ls -lart'
    

    then in your ~/.bashrc:

    . ~/.aliases
    

The second method is better, since it does not limit you to aliases, but also allows defining functions, which are much more powerful.

share|improve this answer
    
That's right. It was a completely wrong approach. I did it myself using these commands: while read line; do echo "alias $line" >> .aliases_full; done < .aliases I then just did mv .aliases_full .aliases . ./.aliases #in .bashrc Thanks anyway :) –  Vladimir Sep 4 '11 at 13:22
add comment

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.