Ubuntu – Bash alias -=… (alias name should be a simple dash) not working

aliasbash

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?

Best Answer

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 -'