1

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?

Damon Swayn
  • 1,326
  • 2
  • 16
  • 36

1 Answers1

0
  • 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 "$@"
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • So the long and short of it is that I can't alias the raw `;s` to `ls` to work around my incorrect typing? – Damon Swayn Sep 20 '16 at 02:53
  • You can't define an _alias_, but you can define a _function_ (or even an executable script in your `$PATH`). – mklement0 Sep 20 '16 at 02:55
  • But I can't call that function without quoting/escaping the semi-colon character? – Damon Swayn Sep 20 '16 at 04:54
  • @DamonSwayn: Yes - I was so focused on finding a _technical_ solution that I didn't realize that having to quote on invocation completely defeats the purpose - I've updated the answer accordingly. It comes back to fighting the shell grammar; I don't think it's worth doing. – mklement0 Sep 20 '16 at 12:36