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.

How do I write this as an alias?

p() {
    DIR=~/projects/$@
    pushd "$DIR"
}

This is as far as I got, but it's piping stdin to pushd instead of xargs:

alias p="xargs printf ~/projects/%s | pushd"

It's probably better as a function, but I feel like I would learn something by knowing how to write it on one line and get the piping correct.

Thanks!

share|improve this question
3  
I think you can't do this in an alias. –  Andreas Wiese Apr 23 at 19:32
    
Why does it need to be aliased? Give the function the name you want to call and use it as it is. Aliases don't take args. –  bdowning Apr 23 at 21:24
    
As mentioned, it doesn't need to be aliased. I'm always looking to learn and expand my understanding, so I'd rather ask than not. Thanks! –  Raine Apr 24 at 13:35

1 Answer 1

up vote 7 down vote accepted

As @AndreasWiese comments, you can't do this with an alias. Aliases tack on arguments space-separated, so you can't join them (in a simple way) to the aliased command.

A comment on your function: Why are you using $@? You should use $1 instead. If you call your function with multiple arguments (p a b c), you'll probably get an error (b: command not found) an unknown directory.

share|improve this answer
    
Thanks! Good to know. I will look up $@ vs $1 because I didn't understand the difference before. –  Raine Apr 24 at 13:35
    

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.