1

I use Windows and Linux a lot, and sometimes I type cd\ from Windows muscle-memory, so I tried to alias that, alias cd\='cd /' but that doesn't work (presumably because \ is an the escape character in Linux). Is there a way, using an alias or a function that I could make typing cd\ => cd / ?

1
  • Maybe you should just try to avoid typing cd\ ? Seem to be the simplest solution. As the joke goes: [client] Doctor, my arm is hurting when I do like this [waving his arm around]. [doc]: So don't do like that!
    – not2qubit
    Commented May 2, 2021 at 10:26

1 Answer 1

2

That would be hard, since the backslash is used to escape the next character, and at end of line, it starts a continuation line. So even if you could make a function called cd\, you'd need to run it as cd\\, or 'cd\'. And with aliases, escaping or quoting part of the name prevents alias expansion...

Anyway, you can't create those aliases or functions in Bash:

$ alias cd\\='echo foo'
bash: alias: `cd\': invalid alias name
$ cd\\ () { echo foo; }
bash: `cd\\': not a valid identifier

You can in Zsh, though, but you need the double-backslash...

% cd\\ () { echo foo; }
% cd\\ 
foo

Actually it even seems to accept the alias, but you can't use it:

% alias foo\\='echo bar' 
% foo\\
zsh: command not found: foo\
% 'foo\'
zsh: command not found: foo\

Bash can run an external command with a backslash in the name, but that doesn't help with cd.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .