11

When I was using openSUSE 11.3, it came with several aliases already set up. Two that I used a lot were + for pushd . and - for popd. Now on Debian, I can't figure out how to create the second one. alias -=popd tries to read -= as a command option to alias. I've tried putting quotes (both single and double) around it, and preceding it with a backslash, but I just keep getting error messages. Any suggestions?

Thanks

2 Answers 2

14

try this:

alias -- -=popd

worked at least for me:

usr@srv % alias -- -=echo
usr@srv % - test
test
4
  • Thanks, works perfectly. What does the -- do, exactly?
    – Wolf
    Apr 24, 2011 at 16:22
  • 5
    @Wolf: For most commands, -- marks the end of options; everything after it is arguments. For example, logger foo -x bar would log "foo bar" and treat -x as an option, while logger -- foo -x bar would treat it as an ordinary word in the message "foo -x bar". Apr 24, 2011 at 16:58
  • 1
    The -- indicates that no more options follow on the command line, following -'s aren't interpreted as options. So this allows you to cat files that start with a - ;-)
    – binfalse
    Apr 24, 2011 at 17:07
  • @grawity and @binfalse: Thanks for the explanation, that's very helpful! Very good to know.
    – Wolf
    Apr 24, 2011 at 17:09
9

In bash you can create functions named as such:

+() { pushd "$@"; }
-() { popd "$@"; }
3
  • what is $@?
    – Lazer
    Apr 24, 2011 at 16:54
  • 2
    @Lazer: In sh and bash, $@ expands to all arguments given to the function or script. Unquoted it works the same way as $*. When inside double quotes, however, it expands to exactly the same words as originally given, without any additional splitting. Apr 24, 2011 at 17:05
  • @Lazer: $@ expands to all the parameters passed to the function (equivalent to "$1" "$2" ...). For more information, do man bash and search for Special Parameters. Apr 24, 2011 at 17:05

You must log in to answer this question.

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