14

I want to create an alias for cd - which should be just a dash (-). I tried without success:

alias -='cd -'     # bash: alias: -=: Invalid option
alias \-='cd -'    # bash: alias: -=: Invalid option
alias '-'='cd -'   # bash: alias: -=: Invalid option
alias '\-'='cd -'  # bash: alias: `\-': Invalid alias name.

The former three are the same (only different input, but bash turns all of them into the same command alias with a single argument -=cd -), so it's no surprise the error message is the same. I'd guess that if the argument starts with a dash, it's parsed as a flag rather than the alias name.

Is it even possible to use - as an alias name?

28

With most commands, you can pass -- as an argument, and all subsequent arguments are treated as operands and not options, even if they begin with a dash. The alias builtin in bash recognizes --.

alias -- -='cd -'
  • 1
    Hah! Didn't know this. Cool, thanks! :) – leemes Jun 3 '12 at 19:37

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.