ZSH:
If you'r using zsh
alias -g ,,=".."
is what you need, but this will cause ",," to be expanded everywhere, not only when used with cd
.
From man zshbuiltins
:
If the -g
flag is present, define a global alias; global aliases are expanded even if they do not occur in command position.
BASH:
If restricted to bash
(as the question is tagged with bash), read the following:
This is a pure-alias solution, as requested, however this will do more than required possibly frustrating the user (see the warning at the end of my post).
Quoting man bash
:
If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.
Therefore it's enough to alias cd
with an extra space (to allow expanding of its next argument) and then alias ,,
to ..
. See that
alias cd='cd '
alias ,,='..'
gives exactly what you need.
Note that this is correct not only for bash
(and its alias
implementation), but all POSIX-compilant shells. Quoting an example from man 1p alias
(the manual does not describe this feature explicitly, only through an example):
Set up nohup so that it can deal with an argument that is itself an alias name:
alias nohup="nohup "
Warning:
As @PeterCordes writes in his comment, it will automatically cause other aliases to expand when written after cd
. It may require you to write cd \grep
if you want to change directory to one named grep
but your grep
is an alias for grep --color=auto
. Without the backslash, cd
will report "too many arguments" error (you can't cd
to two directories at once)!.
$ ,,
would complain, as such:bash: ..: Is a directory
. (In other words, it's not an executable.)alias 'cd ,,'='cd ..'
. Well, you could, but you'd have to run it ascd\ ,,
to make it all one word.)alias -g ,,=..
andcd ,,
works as you expect it to.find / -type d -exec ln -s .. {}/,, \;
... ;-)