Can I use cd../.. as an alias name?

Solution 1:

You can't use slashes in an alias name. Bash allows them in function names, however, so you can make that a function:

cd../.. () { cd ../..; }

You can't use backslashes in an alias or function name. The backslash character quotes the next character, so cd..\.. is parsed as cd...., well before that string is looked up as a command name. If you want to call a command named cd..\.., you need to type cd..\\.., 'cd..\..', or something equivalent. Furthermore, a command name which was quoted in any way isn't considered for alias lookup, so you can't ever use an alias name containing a backslash. Bash doesn't allow backslashes in function names, either. Since cd..\.. is parsed as cd...., you can define a function called cd.... (as above).

If you use the cd command, you'll have to type the space after it, just like any other command. But you can save typing by not typing cd at all. Set the autocd option with the following line in ~/.bashrc:

shopt -s autocd

Then you can type a directory name on the command line, and “executing” that directory will change to it.

~/some/sub/directory$ ../..
~/some$ 

Solution 2:

/ and \ are among the characters which cannot appear in a Bash alias name. From man bash:

The characters /, $, `, and = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.

As a workaround, you can switch to Zsh, which allows both:

% grep 'alias.*cd' .zshrc 
alias cd../..='cd ../..'
alias cd..\\..='cd ../..'
% pwd
/Users/firas
% cd../..
% pwd
/ 
% cd
% pwd
/Users/firas
% cd..\..
% pwd
/