All the commands that we type in the terminal, follow a similar type of structure in the way that you type them. By understanding the structure, you will be able to transition from simply just memorizing commands to actually understanding the common language that all Linux commands use.
All the commands are effectively just little computer programs that are installed somewhere on your computer. For example, date is a program, cal is a program and echo is another program. The command name that you want to use is the first part of the command. Each command has its own unique behavior that we can look up using Manual Pages. Below is the general command structure:
commandName -> options -> input
Shell’s Path
Once the shell knows the name of the program you want to run, it then searches for that program on something called Shell’s Path. Shell’s path is just a path to a list of folders that contain programs. You can actually see your Shell’s path by:
commandName + $PATH. So if you type echo $PATH, it will give you different folder paths separated by a colon. So when you enter
So when you enter a command, what shell would do is that it would start at the very left of the path and look inside that folder for a command echo or cal or whatever you called it. If it doesn’t find it there, it will move to the next one. If the shell didn’t find a folder with that name in any of those folders, the shell would give you an error “command not found“. If you want to find exactly in which fold a command exists, you can use which command. So for example, if you enter which echo, you will get the exact folder for the echo command. This is how your shell knows where to find your command.
Command’s Input
Command’s options
If we want to see the date in universal time we can use the option ‘u‘
If we want to give a command multiple options, they would be one after another. So for example if we want to give the date command option a, b, c, d we would type date -a -b- c- -d. We can also simplify it by doing date -abcd.
Sometimes options can have their own input. Let’s take the command cal as an example. -A option requires input after it. So if we type cal -A 1, it would show us the current month with current date highlighted and 1 month after it as well.
The cal command has another option which is -B, which means one month before current month. So if we do cal -B 1. It would show you the current month with current date highlighted and last month.
So an option sometimes needs to have an input for the command cal, -A means number of months after and -B means number of months before. And these options need input. We can also do Command + options + optionsInput + commandInput. Let’s type the following command into your terminal:
cal -A 2 12 2017
So we are giving the cal command -A with input 2, which means that shows 2 months after. We are also giving it the inputs to show us the 12th month of the year 2017. So in short we are asking to get the calendar of month December 2017 and 2 months after it.
- Command = command-name -options inputs
- command-names need to be on the shell’s search path
- commands operate on inputs.
- options modify a command’s behaviour.