~/ajkule


Basics of using the Linux command line

At its base, a shell simply executes commands. A Unix shell is both a command interpreter and a programming language. Shells also provide a small set of built-in commands. While executing commands is essential, most of the power of shells is due to their embedded programming languages. Shells offer features geared specifically for interactive use rather than to augment the programming language. Bash is the shell for Unix. The name is an acronym for the "Bourne-Again SHell", a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh. Bash is largely compatible with sh and incorporates useful features from the Korn shell (ksh) and the C shell (csh). It offers functional improvements over sh for both interactive and programming use. While Unix provides other shells, Bash is the default shell. It currently runs on nearly every version of Unix.

A simple command is the kind of command encountered most often. It's just a sequence of words separated by spaces. The first word generally specifies a command to be executed, with the rest of the words being that command's arguments. A simple shell command such as echo consists of the command itself followed by arguments, separated by spaces.

$ echo a b c
a b c


The history builtin command provides access to the command history, the list of commands you previously typed. With no options, the history display list with line numbers. The easiest way to recall a previous command is to use the UpArrow key. Each press goes backwards one command through history. Pressing the Enter key will run the displayed command again. You can use the LeftArrow key and RightArrow key to position the cursor for editing. The DownArrow key will go forwards through the history.

A variable allows you or the shell to store data. Variables are given names and stored temporarily in memory. When you close a terminal or shell, all of the variables are lost. However, the system automatically recreates many of these variables when a new shell is opened. In the example below, the echo will display the value of the HISTSIZE variable.

$ echo $HISTSIZE
1000


The value of the HISTSIZE variable is used as the number of commands to save in a history list. To modify the value of the variable, you don't use the $ character.

$ HISTSIZE=500
$ echo $HISTSIZE
500


One of the most important Bash variables is the PATH variable. This variable is used to find the location of commands.

$ echo $PATH
usr/local/bin:/usr/bin:/bin:/usr/local/games:/user/games


The shell will look in each of the directories listed above when you run a command. If the command is found, then it is executed. If it isn't found, then you will receive a command not found error. There are also local variabless. To create a local variable, type:

$ variable2='a b c'

To view the contents of the variable, type:

$ echo $variable2
a b c


The export can be used to make an environment variable.

$ export variable2

The export can also be used to make an environment variable upon its creation.

$ export variable2='a b c'

The type can be used to determine information about various commands.

$ type echo
echo is a shell builtin


Using the -a option, the type can also reveal the path of commands and identify aliases to commands.

Quoting is used to remove the special meaning of certain characters or words to the shell. Certain character has special meaning to the shell and must be quoted if it is to represent itself. For example, suppose you want to print the following: "My PC costs $2000". $2000 is considered variable. What if you don't want to treat $2000 as a variable? If you place a backslash '\' character in front of another character, it removes the special meaning from that character.

$ echo My PC costs $2000
My PC costs 000
$ echo My PC costs \$2000
My PC costs $2000


Single quotes (''') prevent the shell from doing any interpreting of special characters within the quotes.

$ echo My PC costs $2000 and yours $500
My PC costs 000 and yours 00
$ echo 'My PC costs $2000 and yours $500'
My PC costs $2000 and yours $500


Double quotes ('"') will stop the shell from interpreting some metacharacters, including glob characters. Without double quotes, next would list every directory in /usr location, but with double quotes, it is just a text.

$ echo "/usr/*"
/usr/*