Linux Shell Tools to make life easier

Daniel Ekow Melfah
5 min readNov 29, 2021

There is a reason why most of us are more comfortable using Windows and other graphical user interfaces. Interaction with digital objects comes naturally. But what if you land your first job and you are required to get comfortable in the terminal as soon as possible? Or do you find yourself in a situation where you cannot use a computer mouse for some time?

I am putting together a comprehensive introduction to how adjusting to living in the terminal is like. I want to introduce some indispensable tools every shell scripter uses.

What does it mean to live in the terminal?

A lot of operating systems have graphical user interfaces. Of course, GUIs are beautiful and interactive but developers need to be as productive as possible. Living in the terminal means being able to execute your workflow right from the terminal with minimal use of the mouse.

To start living in the terminal, you should get comfortable using vim as your default editor. Using nano is ok, but vim offers a lot of advantages in terms of productivity. You can get acquainted with vim by playing with the vimtutor. Run vimtutor in your terminal to learn the basics of vim. If you would like to have fun while learning, vim-adventures.com provides a game that is fun to play and teaches you how to use vim.

Warming Up

Navigating in the terminal is easy. Use cd (which stands for change directory) to quickly navigate through directories in the terminal.

Sometimes, you can write a very long command, like vim /etc/apache2/sites-available/mysite.conf

and you realize you have forgotten sudo. Write sudo !! to quickly bring up the last command. I can’t stress how this useful tip has saved me countless times. Perhaps you’ve run some commands and want to search through your command history, use CTRL + R to search through your command history.

Tools you might already know

When you are working with others, it is possible you might want to access their laptop via ssh. One thing to note is that if you’re going to access a server with a public IP address, for best practice launch a new server that does not have sensitive information. You can access the server with ssh -R 4422:localhost:22 guestuser@[hostuser_ip]

Troubleshooting and making life easier

Network issues can make your life miserable. Thankfully, the terminal has some tools to help you troubleshoot. First of all, you might want to test host connectivity. Do that with traceroute + ping.

It is best to write a useful script that will automate a lot of things you do. But before trying that, I’m going to introduce some key bash concepts. There are a lot of programming concepts that are also inherited in bash. If you’re familiar with programming, you should know how to use variables. Setting variables in bash is not all that complicated. You explicitly define a variable in bash by doing.

VAR=value

It is worth mentioning that when setting variables in bash, there should be no space between the equal sign and the value.

VAR= value will return an error because the variable VAR will be assigned to empty space “ “, and your intended value will be read as an argument. Another thing worth noting is that it is entirely possible for you to create variables in lower case, var, but in bash convention, we usually define variables in uppercase.

You also need to get comfortable with Permissions. Learning how to set permissions is super important. I’m not proud of how many hours I’ve been burnt by fixing permission issues.

Generally, if you’d like to run a script, you need to make sure the script is executable. Running

chmod a+x my_bash_script.sh will generally allow the script to be run.

The same result can be achieved by running

sudo chmod 775 my_bash_script.sh

It’s a rookie mistake that can be easily avoided.

Text Manipulation

There are some occasions where you’d like to look up information in files. Bash has a useful grep command that can be used to look up information.

For example, use grep to search a file containing a match.

Grepping works well with Piping in bash. Not the piping you’re thinking of. To understand piping, I’ll need to first talk about data streams in bash. There are STDIN, STDOUT, and STDERR. Usually, you send data from commands into files with the regex “>”. Piping is essentially sending data from one program into another. Let’s use a popular example. Maybe you’re trying to find the log of a backend crash that happened in a stack trace on “2020–06–06”.

first, we concatenate(read the file sequentially) the log file with

cat event_log

then we search the output of the file that has been read with grep

but we can only send the data from cat to be grepped by using “|”

so the command becomes

cat event_log | grep 2020–06–06

The output will most likely be crashes that have 2020–06–06 in them.

You can go ahead to set vim as your default editor. Now let’s try to create a simple script that performs some workflows for us in git.

The beauty of bash lies in its simplicity. Let’s say you have a series of tests to run using commands but it is annoying for you to type those commands each time. It’s time for the exciting stuff.

Automating Stuff

We can create a script with

vim useful_commands.sh that contains all of the commands we want to run. In the file, we can add our commands,

cd ~ test/pytest/

pytest first_test.py::FirstPage::FirstTestMethod — gui — report-html

pytest second_test.py::SecondPage::ThirdTestMethod — gui — report-html

[more commands here]

something like this. As you can see, it’ll be annoying typing out these commands each time you need to run them.

After saving the useful_commands.sh file, you need to remember to allow the file to be executed. So you can use

chmod a+x useful_commands.sh

After saving your file, you can always use

./useful_commands.sh to run your script.

All done. The script should work. In Linux, you can always create an alias for running your script by editing the bashrc file and adding the location of your script. You can do that by adding the alias

alias runcommands=”cd ~; ./useful_commands.sh”

Anytime you type run commands in the terminal, all your scripts will be run.

Congratulations! You just executed your own bash script. As you become more comfortable, you get to use more granular commands to achieve exactly what you want. There are tons of resources to help you, you only need to know how to get started.

--

--