I've noticed that I have a tendency to mistype ls
as ;s
, so I decided that I should just create an alias so that instead of throwing an error, it just runs the command I mean.
However, knowing that the semi-colon character has a meaning in shell scripts/commands, is there any way to allow me to create an alias with that the semi-colon key? I've tried the following to no avail:
alias ;s=ls
alias ";s"=ls
alias \;=ls
Is it possible to use the semi-colon as a character in a shell alias? And how do I do so in ZSH?
First and foremost: Consider solving your problem differently - fighting the shell grammar this way is asking for trouble.
As far as I can tell, while you can define such a command - albeit not with an alias - the only way to call it is quoted, e.g. as \;s
- which defeats the purpose; read on for technical details.
An alias won't work: while zsh
allows you to define it (which, arguably, it shouldn't), the very mechanism that would be required to call it - quoting - is also the very mechanism that bypasses aliases and thus prevents invocation.
You can, however, define a function (zsh
only) or a script in your $PATH
(works in zsh
as well as in bash
, ksh
, and dash
), as long as you invoke it quoted (e.g., as \;s
or ';s'
or ";s"
), which defeats the purpose.
For the record, here are the command definitions, but, again, they can only be invoked quoted.
Function (works in zsh
only; place in an initialization file such as ~/.zshrc
):
';s'() { ls "$@" }
Executable script ;s
(works in dash
, bash
, ksh
and zsh
; place in a directory in your $PATH
):
#!/bin/sh
ls "$@"