Writing Scripts In Linux: Tips And Tricks

There are times when you run a script with no idea what you did, and output appears. That is not expertise but bad scripting at play. 

This article will discuss some of the best tips and tricks that can save you a lot of frustration. Not only will you be able to wow your counterparts, but you will also drastically increase your productivity.

Joan Young, a professional essay writer from Advanced writers, a reputable writing service, who has created this article for our blog, says: “These tips are not just for beginners, but even advanced Linux users can find some gems they have been unaware of over the years”.

We will discuss some ways you can write flawless Linux scripts in just a few keystrokes.

1.Auto-completion using the tab key

This is very obvious, but it is essential. When typing, you can just hit the tab key to suggest all possible options in the string you have so far typed. This will make it faster for you to finish code and write it more accurately.

2.Switching to the latest directories

If you are in a long directory path and want to move a different approach, you can switch between the directory paths using the command “cd –. “ This will take you back to your last directory, and you don’t need to retype or copy and paste the directory paths anymore.

3.Going to the home directory

This also seems pretty obvious. You can move to the home directory using the command “cd ~.” Modern Linux directories have a shell pre-configured for the command “cd.” This also saves you some time.

4.Displaying the contents of a directory

Most people are used to the “ls-l” command for displaying the contents of a directory. People know only this command for this purpose, but you can also use the command “11” to indicate the directory contents.

This, however, varies with different Linux distributions and configurations. There are, however, high chances are that you can use this command with most configurations.

5.Running multiple commands

Typically, when running multiple commands, people wait for one command to execute before running the following command. However, you can use “;” to run various commands. This way, you can run multiple commands in a line. You don’t need to wait for previous commands to finish running.

6.Running multiple commands on condition the first was successful

We have just seen how to run various commands, but this new trick ensures that the commands run and fail. For this case, you separate your commands using &&. This is useful when auto-updating your system.

7.Searching for commands used in the past

You might have used very good and complex commands a couple of minutes ago, and you need to reuse them. However, the problem is you cannot remember the command. This is not a problem as you can salvage your command using reverse search.

Just use the keys “ctrl+r” and add any part of the command you remember. This will automatically look up the command history and show you the search term you need.

8.Unfreezing Linux Terminals

You might be in the habit of using “ctrl+S” for saving, but when you use that in a Linux terminal, the terminal will end up freezing. When your terminal freezes, you don’t need to close the terminal. You can use “ctrl+Q” and unfreeze the terminal.

9.Moving to the beginning or line end

You might have a very long command and, while midway, find that you need to change something. You can use arrow keys to the beginning or end of the line. Of course, you can also use the home and end keys, and you can also use Ctrl+A or Ctrl+E to go to the beginning or end of the line.

10.Reading compressed logs without extraction

Server logs are usually compressed to save space. However, this creates a problem for developers when they want to analyze the logs. Sometimes you might not even have permission to extract the logs. However, z commands are a great savior in such situations. They provide regular alternatives to help deal with log files.

If you have problems with z commands and any other Linux commands, you can get professional help from writing companies, where they can write good code for you in detail.

11.Using less to read files

If it is a big file, most people use cat commands to read the screen. However, this is not the best option. You can use other command-based text editors, but less command is a good choice if you just want to read the text. Only input “less path_to_file.” There are terms inside less to allow you to search by page, line numbers, or display.

12.Reusing the last item from the latest command

This comes in handy in very many situations. Say you have just created a new directory, and you want to go back to the old directory, you can use “!$”. You can also use “alt+” and toggle several times to shuffle the options between the commands.

13.Reusing previous commands

You can reuse the entire previous command when writing a script using “!!”. This is useful when running commands and realize that you need to run the root privileges.

14.Fixing typos using the alias

You probably know an alias in Linux, but most people do not realize they can use an alias to fix typos. To fix mistypes you can use the command: “alias incorrect=correct.”

15.Killing running processes

When writing scripts and you have running processes in the background, you can kill them using the command “Ctrl+C.”

There are very many other commands that can help you in writing a better script on Linux. However, the ones listed here are the best since they are available in almost all Linux distributions. You do not need to install new tools to create good code.

You can also replicate code using the alias command, as it saves a lot of time. Writing code in Linux is always enjoyable, and you can also share some of your best experiences and tips and tricks.