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.

In my ~/.shortcuts, I have symlinks to some frequently used directories.

In .bashrc (OS X), I am doing this:

ls -1 ~/.shortcuts | xargs -I {} alias {}="'cd -P ~/.shortcuts/{}'"

When I run alias in the terminal, the output shows the newly added aliases but none of the aliases work. Why?

I have some aliases like alias ll='ls -l' defined in .bash_aliases. Doing . ~/.bash_aliases in .bashrc loads my custom aliases perfectly well. But, when I redirect the xargs -t output to a tmp file and source it in bashrc (just like I source .bash_aliases), it doesn't work.

What am I doing wrong?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

It doesn't work because that alias is run in a subshell that exits immediately after that. It never affects the shell that started the pipeline.

You should try something like:

for short in ~/.shortcuts/* ; do
  alias $(basename $short)="cd -P $short"
done

This assumes you have no files with funny characters (like whitespace) in their names in that folder.

(Generally speaking, don't parse the output of ls, use your shell's globbing functions.)

share|improve this answer
    
About the second part of the question: I'm guessing, sourcing the file with aliases did not work because I was parsing the output of ls? –  harithski Jan 9 at 18:44
    
No, more likely some quoting problems I guess. Would have to see the exact contents of that file to know exactly. –  Mat Jan 9 at 18:46

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.