RHCE Study Guide

This tutorial explains Linux command syntax including I/O (Input/Output) redirection, STDIN, STDOUT, STDERR, File descriptor and piping in detail with examples.

Linux command syntax

Basic syntax for a Linux command is :-

command option argument

Command

Command is the link of an executable file. When we hit Enter Key after entering any keyword at shell prompt, shell finds the associated executable file in specified locations. If shell finds the file, it will execute that otherwise it will return with an error message “Command Not Found”.

Option

Option is a single letter or word, preceded by a hyphen that is used to change the behavior of the command. A command may use single option or multiple options or no option at all. Further options can be used independently or combinedly. Regardless how we use, options will be interpreted separately. To group the options, remove space and hyphen from all options and precedes single hyphen only with first option.

linux command options

To use a whole word as option, usually commands use double hyphen. This convention removes unnecessary confusion between single word option and grouped single letter options.

Let’s understand this a bit more. Mostly commands use --help option to provide the basic information about them. If we use single hyphen, shell will first try to interpret all letters individually (-h –e –l -p). If it fails to execute the command with interpreted options only then it will try to execute the command by assuming all options as a single option (-help).

linux command options

If command supports single word option with single hyphen, it will execute. Otherwise shell will return with error message “invalid option”. Again error message will be displayed for the first unmatched option.

linux command syntax

Shell process options based on hyphen signs. If there are two preceding hyphens, shell will assume that it is a single option. It will try to execute the command with this option. If it is a valid option, command will execute. With two hyphens shell will never process the letter from word separately. With single hyphen every letter from word will be interpreted separately.

Argument

Argument is the keyword which specify the target where command will perform the action. Keyword may be any supportive information such as filename, directory, device or username. A command may accept single argument or multiple arguments or no argument. Arguments are limited by the total allowed numbers of character on command line.

Shell allows us to combine arguments with options to get more specific result. If argument is going to be used with a single letter option, add a space between option and argument. If argument is going to be used with word option, add an equal(=) between them.

linux command argurments

Input / Output (I/O) Redirection

Linux treats every object as a file including input and out process. When we access a file, Linux creates an entry point in Kernel which is used to uniquely identifies that file in running session. This identifier is known as file descriptor. File descriptor is a non-negative integer value. Shell reserves first three file descriptors (0, 1 & 2) for STDIN, STDOUT and STDERR.

STDIN, STDOUT and STDERR represent data flow in Linux.

File Descriptor Name Data Flow direction Default Device
0 STDIN (Standard Input) < Keyboard
1 STDOUT (Standard Output) > Monitor
2 STDERR (Standard Error) > Monitor

STDIN & STDOUT :- User types a command from keyboard (STDIN) at shell prompt and hit the Enter key. Shell finds the command. If command exists, shell will execute it and display the result on monitor (STDOUT).

linux stdin stdout

STDIN & STDERR :- User types a command from keyboard (STDIN) at shell prompt and hit Enter key. Shell finds the command. If command does not exist, shell will display the error on monitor (STDERR).

linux stdin stderr

Beside keyboard, Shell accepts command from various input methods such as software, cron and script. Since these method do not have direct console interaction, we have to specify the location for output. For example a script which automatically executes when a user login and sends the output to the log server. Since shell is receiving commands from the script instead of a standard input device, it must have to know where it should send the output generated from the commands.

linux stdin stdout stderr

For input redirection < symbol is used while > symbol is used for output redirection. When we use input redirection symbol (<) Linux replaces it with file descriptor and read the script and retrieves the commands as if they were typed on the keyboard.

linux io redirect

Common I/O File redirections

Redirector Description
> Store output in specified file. If file exist, content will be overwritten. If file does not exist, a new file will be created with specified name and output will be stored.
>> Store output in specified file. If file exist, content will be appended. If file does not exist, a new file will be created with specified name and output will be stored.
2>&1 Send error messages and command output on same destination.
< Read command from file instead of keyboard.
/dev/null Send output to null. (Discard the output.)
/dev/tty1 Send output to terminal number one. (Require root permission)
/dev/sda Send output to first hard disk (sda). (Require root permission)

Pipes in Linux

Pipes make I/O redirection more flexible. It allows us redirect the output of one command to other command as input.

linux pipes io redirection

Shell allows us to combine multiple commands using pipes. To combine commands use pipe (|) sign between them.

linux pipe examples

When using pipes, output of last command will be displayed in standard output (monitor) device.