I want to explain the linux command line here but without going into any of the actual commands so by the end of reading this, you will have a better understanding of what the terminal is and how it works.
Over the many years that I’ve been using linux, I can’t remember once reading anything that explains the linux terminal to an absolute beginner and so here is my explanation of just what’s going on. Most people already know that this is where you run commands, but I will go over everything that I feel is necessary to know but without throwing a bunch of random commands at you.
Terminal, command line, console, shell & prompt
The linux terminal, the linux command line, the linux console and the linux shell are the all usually referring to the same thing and these terms can be used interchangeably.
So for example, in this post you may hear me refer to it as the terminal at one point, but then call it the command line somewhere else. It’s important to know that these names mean exactly the same thing so don’t get confused by this.
However, it is not common to call it the PROMPT as this name is usually used to describe in the Microsoft DOS (Disk/Dirty Operating System) console.
But… The linux console/terminal/command line DOES actually HAVE a prompt!
The prompt is the text that you see on the top left corner of the window and usually by default this will show something like ‘USERNAME@HOSTNAME $’.
If the prompt ends in a dollar sign “$” then this indicates that you are using the console as a regular user. However if the end of the prompt shows a hash/pound sign “#” then this indicates that you are using the console as an administrator, or in linux we call this the root user.
True terminal, or terminal emulator in a window?
I briefly want to mention what a window is, as it is relevant to what I’m about to explain so you may skip this part if you think I’m being a little too patronising here as it is quite basic.
If you think about Microsoft Windows for a second, every program that is in the foreground and open on the desktop, is in a window.
Now in linux we can have a terminal open in a window just the same as it would be in Microsoft Windows, but we can also access the terminal when we are not in a desktop environment too.
If you are new to linux and you’re using a desktop environment then most likely your system will automatically start the desktop environment. We can configure linux to not use any kind of graphical interface and so we would only see the terminal and we would be greeted with a prompt which is awaiting your command (quite literally).
Bash and other interpreters
When we are presented with that black screen and a contrasted lightly colored text, we may assume that it’s all just the same. But it’s not.
By default, most (if not all) linux distributions use the Bash interpreter right out of the box. For the rest of this post I will only be referring to the bash interpreter but it’s important to know that others exist, and with a simple command it’s easy to change that environment to an alternative shell if need be.
Other shells include (but not limited to) the older bsh, ksh, and csh.
Bash (Bourne Again SHell) had replaced bsh (Bourne SHell), the csh is an environment for the C programming language and ksh is the Korn SHell. ksh is usually found as the default shell for UNIX systems.
It is usually possible to see what shell you are currently using by entering the command “echo $0” from the terminal.
Working around Bash & keyboard shortcuts
In the Bash environment then, we can use some keyboard shortcut’s that will greatly help us in carrying out our commands more quickly. The two shortcut’s that come to mind and which I use very often is the up and down arrow keys which is used to shuffle through your command history, and the TAB key which is used for auto-completion.
Auto-completion is a great feature that saves us so much time by completing our commands so we don’t need to remember an exact path to a file or remember exactly how it is spelled or if any uppercase characters exist etc.
Also if we are running our terminal in an emulator (in a window) then we can use CRTL+SHIFT+T to spawn another terminal window.
We can also clear the screen using CRTL+L. This works the same way as using the ‘clear‘ command.
There are many more keyboard shortcut’s available in the linux shell and hopefully I will cover these in their own topic sometime in the future.
How your command is understood by the shell
I’m going to give a brief explanation here on how the bash interpreter interprets the command that you are giving to the system.
So the very first word we type in to the command line is the name of a program. If the command is not in the system then the reply from the console will be an error message indicating that that particular command either doesn’t exist or isn’t installed.
After the name of the command is entered, anything else we type after the command name is our options for that particular command that we have chosen.
Some commands require a directory path to be given, some require a file name to use as an input or an output for example.
Just a note here that no one knows every single command and every single option available for each command as there are so many variables here that it would be impossible to remember everything. Fortunately for us, we have help.
The man and info commands come in useful very often and you will be using this regularly. Sometimes we can just enter the command name into the terminal and we will get output on just how to use that command.
command options have many names too, such as ‘flags’ or ‘switches’. Don’t get confused by this as they generally are referring to the same thing.
We can usually specify multiple options like this: “-a -b“. We can further shorten this by simply stating “-ab” as this is also passing both the a and b options to that command. As I stated before, you would need to check the manual page of a given command to find out what options are available.
In linux (but not usually UNIX) it’s possible for some commands to have human-readable options such as “–sometext” for example. This is just two hyphens followed by a word or two. This just makes remembering command options easier for some people.
I’m only going to briefly mention redirects here as this is a very powerful feature of linux commands.
When we execute a command on the terminal, by default the output of the command is displayed on the terminal for us to see and read.
But it’s possible to send the output to a file instead. It’s also possible to send the output to BOTH the terminal AND to a file at the same time by using the ‘tee‘ command for example.
To send an output from one command to the input of another command we use what’s called a pipe. The pipe symbol is “|”. Don’t get this confused with sending our output to a file though as the right angle bracket “>” is used for this. However, this will over-write any contents into the given file name. Sometimes we may want this, and other times we don’t. We can append our output to the file by using double right angle brackets “>>” instead.
Another possibility here is to redirect our error messages in one direction (to a log file for example) but allow our standard output to be displayed in the terminal.
Writing your own script
shebang (The first line)
Because linux has multiple shell interpreters (bash, bsh, ksh, csh etc), our script will be interpreted in different ways depending on what interpreter is running the script.
Because Bash is the default interpreter I will explain how we make sure it is bash that runs our script. If we write a script that is intended for bash, but is ran by korn, then most likely our script would seem broken as korn cannot understand (or interpret) it.
So the very first line at the top of our script we will place #!/bin/bash
This is what is known as the shebang. In this case then, we have a bash shebang. If for example we wanted our script to run in the korn shell, we would have a korn shebang that looks like this: #!/bin/ksh as the first line in our shell script file.
When we execute our shell script then, the system sees this shebang right away and then runs it through the correct interpreter.
Just a quick note here: The ‘bin’ directory is where some of our binary executable programs are stored. Our shell interpreter executables (bash, ksh etc) are stored in this bin directory. Common linux tools that do not require admin/root privileges to run are usually stored in bin. If a particular tool DOES require root privileges to run then it would usually be found in the sbin directory (meaning system binary).
One thing to note here is that when ever you start a command with the ‘#’ symbol this tells the interpreter to completely ignore everything after it on that one line. (we usually write comments next to commands as it makes it easily readable to humans) but the shebang is the only line where this is not the case. That line is in fact read.
Using the ‘exit’ command in a shell script
When our script finishes running, the “exit 0” tells the interpreter that the script had executed successfully. Sometimes we may want our shell script to exit early. In this case we can use “exit 1” or any other number we want. This would let the interpreter know that a problem had occurred somewhere while we were executing our script and that it did not complete successfully.
And that’s it! Not much to it is there? Okay so I haven’t given you a 500 page in-depth explanation of the linux shell, but this wasn’t my goal here.
Hopefully I have covered everything that’s needed to know to get you going but it’s very possible that I may have missed something worth noting.
Rather than just firing off random commands to you here, I have hopefully explained everything you need to know as a beginner about what the linux shell is all about and just what is happening behind that black screen with unusual looking text.
Don’t be afraid of using the terminal and I can guarantee that the more you use it and start learning more commands, then you will enjoy using it so much. This is where the power is in any computer system but you must be willing to put in the time and effort to harness that power. And believe me, linux is very powerful!
If you have any comments or questions then please let me know as I am always looking for ways to improve upon these posts. Thanks