Super User is a question and answer site for computer enthusiasts and power users. 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

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
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"

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 '14 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 '14 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .