Shell is a command-line interface that interprets a user’s commands and script files. It tells the server’s operating system what to do with users' commands. Several shells are widely used, such as the Bourne shell (sh), Korne shell, Bourne-Again (Bash), and C shell (csh). Each shell has its own feature set and intricacies, regarding how commands are interpreted. They all feature input and output redirection, variables, and condition-testing.
After you login to a server or HPC cluster such as Discovery, you will be dropped into the command prompt, or shell prompt. Now, you can write your commands.
Here is an example of the command prompt.
Here is a breakdown of the composition of the command prompt.
user- The username of the current user
hostname- The Hostname of the server
~`- The current directory. In bash, which is the default shell, the ~, or tilde, is a special character that expands to the path of the current user’s home directory; in this case, it represents /home/user
$`- The prompt symbol. This denotes the end of the command prompt, after which the user’s keyboard input will appear.
The prompt can also end with the following:
Bash is a command language interpreter that was written by Brian Fox for the GNU Project as a replacement for the Bourne shell (sh). It offers functional improvements over sh, for both programming and interactive use. Bash is the default shell in most Linux distributions. It supports some advanced features like wildcard, piping, command substitution, variables, and the history of commands entered. It also supports the tab completion, where you can type just enough of the filename to uniquely identify it and then press the Tab key. Bash will automatically complete your command.
There are many standard Linux commands and utilities that are installed with the OS. It allows you to navigate the file system, install software packages, and configure the system/applications. A running command is known as a process. Commands can be executed using the following modes:
Foreground - It’s the default way that commands are executed. When a command is executed in the foreground, the user must wait for the process to finish before being returned to the command prompt, at which point they can continue executing more commands.
Background - Unlike a foreground process, the shell doesn’t have to wait for a background process to end before it can run more processes. To run a command as a background process, you can use
&, after the command to be executed as follows:
Instead of making you wait for the command to finish as in foreground, you’ll immediately be returned to the shell. Now, you can execute another command for either the foreground or background process.
Commands can be executed by the following ways.
Without arguments or options - to execute a command without any arguments or options, simply type in the name of the command and hit RETURN.
With arguments - many commands accept arguments, or parameters, which can affect the behavior of a command. For example, cd command can be used to change the directory. Here, you need to pass it a single argument that specifies which directory to change to.
With flags - using flags change the behavior of the command. Flags start with
-followed by individual upper- or lower-case letters. Also, they may start with
--followed by a word (usually a descriptive word). For example,
lscommand can be used to list the content of a directory. A flag
lcan be used to print a long listing. To use this flag with
lscommand, you can simply type,
With arguments and flags - arguments and flags can be combined when running commands. For example, if you would like to check the content of
homedirectory including the hidden files, you simply type the following.
ls -a /home
A variable is a pointer to actual data. The shell enables you to create, assign, and delete variables.
The name of a variable can only contain letters (a to z or A to Z), numbers (0 to 9), or the underscore character(
A variable name can begin with letters or underscores but not numbers.
You aren’t required to define any data type at the time of variable declaration.
To create a variable in the shell, use the following pattern.
No whitespace is allowed between the variable_name, the equals sign, and the value.
For example, to create a variable named
my_cluster and assign the value
discovery to it, simply type the following.
To access the value stored in a variable, prefix its name with the dollar sign ($).
For example, to print the value of
my_cluster, you need to type the following.
Linux environment variables act as placeholders for information stored in the system that passes data to programs launched in shells or subshells.
To list all the environment variables in the shell, you simply type,
To view the value of a variable, you can use
echo command followed by
$ variable name. For example, to view the value of
PATH variable, enter the below command.
To set or update the value of an environment variable, you can use
export command. It exports a variable and hence, it will be inherited by child processes.
VAR is the variable name, value is the new value of the
VAR. For example, to set the value of the
HOME variable, enter the following command.
Sometimes you would like to update a variable and keep its original value. For Example, to append
/home/tool/bin to the PATH variable at the end, you simply type,