DEV Community

Cover image for Don't panic when using CLI
Eduardo Lisboa
Eduardo Lisboa

Posted on

Don't panic when using CLI

Intro

When using the Command Line Interface, we often find a few long and / or confusing commands. And this post intends to ease the pain of getting along with that.

When things start to get complicated, there are some things that can be done. In this post, we are talking about three of them:

  • Aliases
  • Functions
  • Comments

Development

Aliases

An alias is exactly what it means: a nickname. It is very useful when running commands that take too much arguments. For example:

alias lah=ls -ltrah
Enter fullscreen mode Exit fullscreen mode

Now, when running the lah command, it will be exactly like running ls -ltrah.

Functions

A function is probably the best way to run a group of commands. When adding a if or a for statement, it gets even more interesting. Let's suppose we want to connect to a server which is still being provisioned or rebooted:

function keeptrying() {
    until $@
    do
        sleep 1
        echo -e "Trying to run $1 again"
    done
}
Enter fullscreen mode Exit fullscreen mode

Now, this function can be called like this:

keeptrying ssh myserver -l myuser
Enter fullscreen mode Exit fullscreen mode

Due to the until statement, this function will keep trying forever to run any command given, until it is successful. Possibilities are infinite, limited only by our imagination. Be creative.

Comments

This resource is more useful when searching for commands on history logs. While aliases can be useful to shorten a complex command, comments can be useful in another way: finding those damn complex commands. When relying on CLI, a lot of useful commands are run and often revisited. For this, we can use Control + R and start typing the command we want to find. But... what if we could search for it's description instead of the command itself? Very useful when we can't even remember the command, isn't it?

All we have to do is:

ls -ltrah # list hidden files
Enter fullscreen mode Exit fullscreen mode

Now, we can revisit this command by pressing Control + R and typing list hidden files, or even only hidden. Pressing Control + R again will show the next other search result. Pretty neat, isn't it?


Conclusion

There are plenty of things we can do by mastering such techniques. As said before, the most important thing is to be creative. First, start by playing with these guys until getting used to them. Then, start doing real things that will change your life forever.

Hope to have helped.

Cheers.

Top comments (0)