Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can I make an alias beginning with `:` colon, but not `;` in zsh?

I know I can probably get around this by defining a function, but I was curious why the semicolon gives me so many bugs?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

; is a special shell character used to separate commands; for example this:

% ls /; ls /etc

Is (mostly) the same as if you had typed:

% ls /
% ls /etc

A : on the other hand is merely a special shell builtin, and can be overridden.

There may be tricks to override this; you can’t "just" override it with functions either, but I would strongly recommend against it as I suspect it will only bring you confusion and errors. There are plenty of other characters on the keyboard that can be used 🙂


That said, you can use a global alias to override ; to call a function:

% alias -g ';'='semicolon'

% semicolon() { print -r -- semicolon: $@ }
% ;hello
semicolon: hello

You’ll have to modify that semicolon function though.

Note this will apply everywhere on the commandline, including in the middle of a command:

% ls ; ls 
ls: cannot access 'semicolon': No such file or directory
ls: cannot access 'ls': No such file or directory

You could modify the semicolon() function a bit to be smart about at least some scenarios, but I would strongly recommend against it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading