How do I write this as an alias?

p() {
    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.

  • 3
    I think you can't do this in an alias. Apr 23, 2014 at 19:32
  • 1
    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.
    – bsd
    Apr 23, 2014 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! Apr 24, 2014 at 13:35

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.