quick fix: fish function

Published on 16 Nov 2018

A calm morning just before I packed for lab/school/work, I installed Docker (docker.io package in Ubuntu Bionic) because later I thought I would like to play with Emby (more on that later). It has been awhile since the last time I used the sudo systemctl command, and I remember a Bash function that I wrote not too long ago that made the systemctl command much more logical to me.

The default systemctl, for example, to start docker.

sudo systemctl start docker

To me, this feels kind of long. Also, it does not feel logical to me. Why a status comes before the service? Shouldn’t it be like this?

sudo systemctl docker start

Because, IMO, linguistically it makes much more sense. To remedy this, I wrote this simple Bash function as a part of my .zshrc file.

function control() { sudo systemctl "$2" "$1" }

This defines a new function control(), then reverses the order for the status and service. I think it is easier to just show you. To use the same example as before, i.e. starting docker service, now I type:

control docker start

The command is much shorter now (since sudo is included as well) and it makes sense, at least to me.

Well, I bumped into a relatively quick problem to fix this morning: the fish shell uses a different syntax for function. Instead of being written as the Bash function shown above, the fish equivalent for the same function is written as follows:

function control
  sudo systemctl $argv[2] $argv[1]

It looks kind of neat. From a thread on Stack Overflow, the $argv is a list, and to access a range of elements, you need to slice it with square bracket, as shown in the function above.

It works. That’s all that matters now. On the other hand, I should really spend time learning Bash shell, and to some extent, Fish shell.