April 11, 2017

The Linux Command Line

Linux Command Line

The Linux command line provides a way to manually interact with the Linux operating system. The shell program is a program that acts as an interface between the user and the rest of the Linux operating system, including the kernel.

The shell displays the shell prompt. Users enter commands at this prompt. By default, the shell display one of two prompts, depending on the type of user logged in.

For root users, the prompt is the # symbol:
 
For non-root users, the prompt is the $ symbol:
 
The shell accepts the commands and processes it. The Linux command line refers to commands entered at the shell prompt.

The command line ends when you hit the Enter key.
A command line however can be extended beyond a single line. I.e. if the command line is longer than one line, you can use the backslash to extend the command line to two or more lines, e.g.

sudo docker run -v /home/user1/foo:/home/user2/src -v /projects/foo:/home/user2/data \
-p 127.0.0.1:40180:80 -p 127.0.0.1:48000:8000 -p 45820:5820 -t -i user2/foo bash

When the shell sees the backslash, it knows to ignore the Enter key and expect more arguments and/or options.

There are many shells in Linux. A commonly used shell is bash, the Bourne Again SHell. When you start a Linux container in Docker, you can specify which shell it should run, e.g. $ docker run --rm -it debian bash.

The Linux command line consists of three main types of objects: command, argument(s), option(s).
Command is the application/program to run, e.g. ls, perl, docker, docker-compose, etc. The command is always the first object on the command line.

There is normally one command object per command line. An exception is if you have pipes (|). A pipe allows multiple commands to be run in series on the same "command line". More on pipes in a later article.

An argument is a parameter or sub-command used to provide command with additional information, e.g. by itself, the ls command lists the files or directories in the current directory. To list files in another directory, you can enter the other directory as an argument, e.g. ls /opt/bin. A command line can have zero or more arguments.

Options are used to modify the behavior of the command. E.g. the ls command will display visible files/directories. Given the -a option, e.g. ls -a, it will display both visible and non-visible files.

Options come in two forms: short-form, typically prepended with a single dash, and long-form, prepended with two dashes. Examples:
  • short-form option: ls -a or docker ps -a
  • Long-form option: ls --all or docker ps --all
There can be zero or more options per command line. Use a space to separate multiple options. You can mix and match short-form and long-form options on the same command line:
  • ls --all -l
For the short-form notation, you are allowed to concatenate the options. I.e. instead of ls -a -F -l, it's OK to combine the options, prepending the set with a single dash, e.g. ls -aFl.

An exception to being able to combine options is if the option requires an argument, i.e. the -v option in Docker requires the volume path or directory as an argument, e.g. docker run -v /data, as such it should stand by itself.

No comments:

Post a Comment