The Beginner’s Guide To Shell Scripting: More Basic Commands & Chains

Shell scripting is an important part of Linux and is very useful for executing commands. In this section we will discuss a few basic commands along with text file manipulation. Both are an integral part of shell scripting and knowledge of these can help you prepare and execute your commands better.

Some Basic Commands that Can Be Used During Shell Scripting:

The command-line is a very useful aspect of programming. Among the command-line, redirection is the most important command. By employing redirection as a command one can easily use the output of every command. We can use this output for furthering the program or apply the output as input for a different command. Alternatively, the files can also be used as inputs.

Let’s look at some of the popular and regularly used commands.

1. Echo:

The command is used for displaying the complete discourse of the program over the command-line. It acts as output.
An echo command works mainly by placing a backlash (\) ahead of the character.

Even quotes and variables can be used along with echo command. Use of special characters makes it easier for executing commands.

2. Grep:

Grep is short form of Global/regular Expression Print. It is a very useful and efficacious command in Linux. It simply goes through a file and is capable of displaying a line type that is in conjunction with a specific pattern.

The pattern is dependent on a synoptic line which can give multiple patterns for matching. If you do not wish for this default matching then simply enter the tern command.

Grep is a case sensitive command but you can use “-i” flag if you don’t want to get into the hassle of shift and control buttons.

Also Read

Beginner’s Guide To Shell Scripting: The Basics

How to Redirect Outputs?

The symbol of greater-than (>) is used for redirecting the output of a command file. The symbol is a special character and is very useful in redirection of output commands.

What is Piping/Chaining?

Piping is a command that is names after the symbol of pipe (|) that it uses for executing commands. The basic job of piping is to directly transfer the output of one command to another. A series of commands can be prepared in this manner to get a single output. This feature makes piping very useful for commands like grep.

Piping is very similar to “>” in execution. However, one can create multiple chains with piping and without taking the trouble of scanning texts.

Knowledge of regular commands can help you come up with the accurate search commands. As long as the commands are put in proper quotes, they can be used successfully to execute programs. You can even sort the commands and not necessarily in alphabetic order. Look at some of the other ways through which you can sort the commands:

  • tsort – this is an advanced form of topological sorting
  • tr – this allows intra-mapping between characters
  • uniq – this gets rid of the duplicate files
  • awk – this is an advanced form of text processing language. It is mainly employed for distinguishing files based on their types.
  • cut, paste/join – this command is used for separating text files from fields. It allows addition of new data in the columns.