How To Use Linux Shell

by Wire Tech
Linux Shell

Welcome to our another article in the Linux System Administration Series.

For you as a UNIX/Linux system administrator, the shell is so important as if it were both your right and left hands, together!!

Actually, the Linux shell is your own small world where you spend most of your work time: making your daily servers’ health check, investigating & analyzing a strange event in the system logs, developing a new shell script, scheduling a job to run automatically at certain time, freeing some space on a fully-occupied filesystem, installing an urgent security patch, or rebooting / shutting down your machine for maintenance. For these reasons, mastering the Linux Shell should be one of your top priorities.

In this Article, we are going to get our hands dirty working on Linux Shell. Enjoy !!

After having our new Linux server up and running, we first get the login prompt that asks for username.  Type root as username and press Enter.

Linux Shell

The prompt then asks for the password. Type redhat and press Enter.

If you have not installed your Linux box yet, please refer to the previous article: How To Install RedHat Enterprise Linux 9 – Step by Step Guide

If you have not setup your own private lab environment, please refer to this article: Best Way To Set up a Virtual Lab Environment

As a system administrator, having your own lab is very important where you can test new versions of operating systems, new tools, new packages, etc.

You get the shell prompt.

image 19

Now, our journey starts.

As I told you in a previous article: The UNIX/Linux Shell is an interactive program where the UNIX/Linux administrators and users can enter commands and get their results.

The shell is the startup program for most of users. (We will discuss the topic of Linux users and groups in detail in a separate article).

A Linux shell is the way to talk with your Linux server. You can think of the Linux Shell (and UNIX Shells as well) as a translator whose job is to wait for you to talk, take your words, and translate them instantly to the language (form) that your Linux “machine” can understand. When the Linux machine replies, the Linux shell does the opposite: take the reply, translate it into the form that humans can understand, and present that result onscreen.

The Shell Prompt

You have already noticed the following written on your screen:

image 20

This is what we call the Shell Prompt. You enter the commands in this prompt, get the results back, and guess what: you get the prompt displayed again.

Assume we need to know or display the server name (host name) of our Linux box. To do, type the command hostname at the shell prompt and press Enter.

image 21

The hostname of our machine (that we set during installation as you remember) is displayed as RHEL9-srv , and the prompt is presented again (as if the shell is saying: here is the result of your command. Always at your service if you need anything else, sir!!)

Back to the prompt: have you noticed something?

Yes, exactly!! The hostname RHEL9-srv is part of the shell prompt displayed. What else?!

Right!! The root = the user we used to log in to our machine.

So, the shell prompt displayed to the admin / user consists of:

  • The username who started this shell: in our case root.
  • The hostname of the Linux machine.

They are used in the following format:  username@hostname

But what about that ~ ?

This symbol is called Tilde. The ~ or Tilde in the UNIX / Linux shells represents the user’s home directory (which is the current directory for the user when he logs in). If the administrator decides to move to another directory, the prompt will display the new (current) directory.

So, the full format of the Shell Prompt becomes:

[username@hostname  currentdirectory]#

# is the symbol after which we type our command. # is displayed in the prompt when the shell is started for root user. For other users, $ is displayed in the prompt instead.

  Note #1   The switching between $ and # in the prompt as the logged in user changes (from normal user to the superuser root) is meant to help the user / administrator take care when typing command(s) that the command you are about to execute will run as super privileged user.  
Note #2   The root user has the full authority, privilege and power to destroy the operating system, literally!! So, starting a shell with root user account is not recommended. And switching to root should be done only when commands that require administrative privilege need to be executed. As soon as the need to run privileged commands is over, the administrator should log out from root shell.
Note #3   Don’t confuse the # (privileged shell prompt) with the # used to comment (prevent execution of) a command. Preceding a UNIX / Linux command with # causes the shell interpreter to ignore that command, and not execute it.

Linux Shell Types

The default shell for Red Hat Linux is the bash (bourne again) shell. Other types of shells are:

  • Bourne Shell (sh).
  • Korn Shell (ksh).
  • C Shell (csh).
  • Z Shell (zsh).

Shell Variables

There is a list of variables that control the look and behavior of the Linux shell. Such variables are called Environment variables. To display the environment variables and their current values, type the command env and press Enter.

image 22

Because the command output exceeds one page, I piped the output to the more command to display one page at a time (Piping will be discussed in detail in a later article).

From the output shown above, we can see the following shell environment variables and their current values:

  • SHELL           The full path of the executable program of the current running shell. In our case, it is a bash shell whose binary executable is /bin/bash
  • HISTSIZE      The size of the shell history file: which is the number of commands that this shell will remember and keep as history.
  • HOSTNAME The machine’s hostname, which is RHEL9-srv.
  • PWD               This variable stores the path of the current working directory.
  • LOGNAME    Gives the username who started this shell.
  • HOME            Gives the path of the user’s home directory.
  • LANG             Gives the language setting of this shell.
  • TERM            Gives the Terminal type.
  • PATH             This variable stores the paths in which the shell interpreter will search for typed commands.
  • PS1                This variable determines how the shell prompt looks like.

To use or print the value of a variable, you have to precede it with a dollar sign $ .

Consider we need to check the terminal type and language setting for our shell. Type the following command and press Enter:

echo $TERM $LANG
image 23

The values of the two variables have been printed on the screen.

If you forget to put the dollar sign before the variable name in the last command, the shell interpreter will print the variable names TERM and LANG literally:

image 24

On the other hand, setting value for a variable doesn’t require the leading dollar sign with the variable name. For example, let’s change the variable that specifies the format of the shell prompt and see if that would make a change. Run the following command on your shell:

PS1="# "
image 25

Did you see that?! Changing the variable PS1 immediately changed the format of the shell prompt.

Variables are essential in shell scripting. So, we will tackle the shell variables topic in much detail in our Shell Scripting series.

Using Commands

We said that a Linux shell is an interactive program where the administrator can execute commands and get their outputs. What are these command? And how they look like?

A UNIX / Linux Command is an executable software program that contains a list of instructions (directives) that the shell interprets and executes. Typing and executing Linux commands is the way that a system administrator or user controls and interacts with a Linux machine.

So, every Linux operating system comes with a list of available (supported) commands that administrators and users can use. Unavailable commands can be installed later (as software packages), or may not be supported for this specific operating system distribution at all.

Syntax of Linux Commands

A Linux (and UNIX) command follows a common syntax:

command [option]… [argument] …

Where:

  • command is the Linux command to be executed.
  • options are optional (zero or more) execution instructions that determines, changes, or controls the command behavior and/or how its output is formatted/displayed. As the name implies, command options are not mandatory. They are preceded by one hyphen – or two —
  • arguments an argument is the object that the command is going to manipulate. It could be file name, directory, a text to be printed. A command can take one or more arguments, or no arguments at all. Some commands may (or must) take one or more argument(s), while other commands don’t take or need arguments.
Note  #1   In a command definition or manual page, when an item is enclosed by square brackets [ ] , this means that this item is optional. This applies for both options and arguments as well.  
Note #2   When options or arguments are followed by trailing triple dots … this means the command can accept one or more options / arguments.    

So far in this article, we have already used three commands: hostname to display the machine name, env to print the environment variables and their values on screen, and echo to print text and variable values on screen.

The $? and $$ Variables

There are two important special shell variables that you will need to know: $? and $$.

$?

This variable gives the exit code (execution status) of the last command executed. A 0 (zero) exit code means the last command was executed successfully, while non-zero exit value means unsuccessful command execution.

$$

This variable gives the process ID of the current shell.

Each running process (program) has a process ID (number). More about Linux processes will be discussed later in detail in separate article.

The Linux shell is very rich with many nice features that are very useful to system administrators. These feature help the administrator do his tasks in a quick, easy, and efficient way. Learning the Linux shell features like Bash shell commands auto-completion, previous command recalls, and wildcard expansions will be the subject of our next article for this Linux Administration Series. See you there.

You may also like

Unlock the Power of Technology with Tech-Wire: The Ultimate Resource for Computing, Cybersecurity, and Mobile Technology Insights

Copyright @2023 All Right Reserved