I am using Windows and Ubuntu in parallel and do console stuff on both of them.

On Windows I can do a


However, on Linux this does not work, neither with backslashes nor with forward slashes.

21:17:10 $ cd../..
bash: cd../..: No such file or directory

Since I often make that mistake, I have tried to define an alias in .bash_aliases for that situation:

alias cd../..='cd ../..'
alias cd..\..='cd ../..'

When I start a new bash, I get

21:16:26 $ bash
bash: alias: `cd../..': invalid alias name

How would I set up aliases for cd..\.. and cd../.. correctly?

The cd..\.. seems to have problems with the escape character:

21:39:43 $ cd..\..
bash: cd....: command not found

It does not work, even if I define another alias for cd.....

  • 2
    You can't. A command, alias or filename cannot contain the / character. The \ version, on the other hand, should work fine. – fkraiem Sep 27 '14 at 21:31
  • @fkraiem: It seems the backslash version has problems as escape character. Updated the question. – Thomas Weller Sep 27 '14 at 21:41
  • Oh, indeed. From man bash: The characters /, $, `, and = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. – fkraiem Sep 27 '14 at 21:45
  • Damn. I tried man alias and it didn't exist. If you give that as an answer, I'll accept it. – Thomas Weller Sep 27 '14 at 21:51
  • Aliases are one thing, shell functions are another. Have another look. – alexis Sep 27 '14 at 23:24

/ 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
% cd../..
% pwd
% cd
% pwd
% cd..\..
% pwd
  • so alias cd..='cd ..' works, it enough for me – bcag2 Mar 25 at 17:00

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$ ../..
  • If I do the shopt thing, I'll probably miss that functionality on Windows soon :-) – Thomas Weller Sep 27 '14 at 22:01
  • Um, that's not true. Slashes are accepted in bash function names. – alexis Sep 27 '14 at 23:22
  • @alexis Ah, right, you can use slashes in bash. Fixed, thanks. – Gilles 'SO- stop being evil' Sep 27 '14 at 23:28
  • @ThomasW. Of course you will. autocd was one of the reasons I switched to zsh, back when bash didn't have it. The only decent way to use Windows is to install Cygwin anyway, complete with zsh. – Gilles 'SO- stop being evil' Sep 27 '14 at 23:29

"You can't use slashes in an alias or function name"? That's news to me. I've been using the following shell function for years on Linux and OS X bash, and never had any trouble:

function ../.. { cd ../..; echo "$PWD"; }

Naturally, cd../.. is a fine function name as well. A slash can be used in a shell function name.

As for the backslash version, you can't embed it in an alias or function name but you don't have to: When you type cd..\.., bash interprets the backslash as an escape and tries to execute the resulting cd..... So define that and you can do everything you were after. You are welcome.

function cd.... { cd ../..; } 

If you want to keep the aliases, why not just do:

alias   ..='cd ..'
alias  ...='cd ../../' 
alias ....='cd ../../../'

Or, if you need to go way back:

alias   .3='cd ../../../' 
alias   .4='cd ../../../../'
alias   .5='cd ../../../../../'
# how far back do you need to go on a normal basis..?

No idea how well that helps you keep mentally in sync with your Windows brain, but this still seems like a simple way to go. (At least for those of us lucky enough to not need to worry about how things are done on Windows.)

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.