Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Never knew about cd - –  Marco Ceppi Jun 4 '12 at 0:01
    
@MarcoCeppi Me neither, until today. Checkout commandlinefu.com/commands/browse/sort-by-votes for more nice tricks! –  leemes Jun 4 '12 at 0:45
add comment

1 Answer 1

up vote 14 down vote accepted

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 -'
share|improve this answer
1  
Hah! Didn't know this. Cool, thanks! :) –  leemes Jun 3 '12 at 19:37
add comment

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.