0

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?

1 Answer 1

5

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.)

2
  • 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, 2014 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, 2014 at 18:46

You must log in to answer this question.

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