iStock-1075599562.jpg

Developers: How to Extend Your Shell with Custom Commands

By Alexey Anufriev
3 min read

During development, we at Celonis type a lot in our IDEs. The good thing about this process is that IDE supports us here a lot. Things like shortcuts, live templates, and other features help us get more code in, with less effort.

You do not need to rename class across all places - just hit a shortcut instead. Or, if you need a public constant of a String type in your class, just type psfs and hit Tab. Lastly, if you do not know how to do this or that, just hit Shift key two times and ask IDEA.

Besides IDEs, we also type a lot in the shell (whatever you use, but I prefer zsh). I believe it would be cool to have similar level of convenience there, as well. There are plenty of existing extensions for it that can make your life easier, like kubectx, or fubectl for K8s, ect. However, sometimes it is not that easy to find an extension that does exactly you want, and gives you exactly what you need daily, because it may be super-specific. To overcome this, you have the possibility of extending your shell with custom commands.

The shell can easily be extended with custom code using two approaches: aliases and functions.

Extending the Shell with Alias

It is kind of a shortcut for another command. Letโ€™s say to check the content of the directory with all hidden files, with human-readable size, in a list view, you would need to run something like this: ls -laGh. It is not that long, but you need to remember all the options.

Or, an even easier way is to simply add something like alias ll=โ€™ls -laGhโ€™ to your ~/.zshrc (or ~/.bashrc) and just be able to type $ ll.

alias

Extending the Shell with Functions

One problem that cannot be solved with aliases is that they cannot accept arguments. But sometimes you need to run parametized command and would be nice to make a shortcut for it as well. And the solution here is functions!

For example, from time to time I wonder why I cannot run this or that docker image, it fails with โ€œport is already in useโ€œ. And here I need to run another command (I prefer lsof) to figure out what is going on there. And those commands are usually quite verbose and may have lots of options or be stacked with a pipe.

To implement an alias for a command that needs an incoming arguments you can define a function inside your ~/.zshrc (or ~/.bashrc) and call it by name passing some values.

Here is my function from the example above (figuring out ports usage):

extend shell functions example

functions

This is how easy the shell can be extended. Happy coding!

Did you like this article?

Maybe others you know will also find it helpful.

About the author

Letโ€™s
get
started

Insights to inbox - Monthly newsletter

We've received your submission
Please fill in all the fields

Dear visitor, you're using an outdated browser. Parts of this website will not work correctly. For a better experience, update or change your browser.