Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Linux for System Administrators

You're reading from   Linux for System Administrators Navigate the complex landscape of the Linux OS and command line for effective administration

Arrow left icon
Product type Paperback
Published in Sep 2023
Publisher Packt
ISBN-13 9781803247946
Length 294 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Viorel Rudareanu Viorel Rudareanu
Author Profile Icon Viorel Rudareanu
Viorel Rudareanu
Daniil Baturin Daniil Baturin
Author Profile Icon Daniil Baturin
Daniil Baturin
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Linux Basics
2. Chapter 1: Getting to Know Linux FREE CHAPTER 3. Chapter 2: The Shell and Its Commands 4. Chapter 3: The Linux Filesystem 5. Chapter 4: Processes and Process Control 6. Chapter 5: Hardware Discovery 7. Part 2: Configuring and Modifying Linux Systems
8. Chapter 6: Basic System Settings 9. Chapter 7: User and Group Management 10. Chapter 8: Software Installation and Package Repositories 11. Chapter 9: Network Configuration and Debugging 12. Chapter 10: Storage Management 13. Part 3: Linux as a Part of a Larger System
14. Chapter 11: Logging Configuration and Remote Logging 15. Chapter 12: Centralized Authentication 16. Chapter 13: High Availability 17. Chapter 14: Automation with Chef 18. Chapter 15: Security Guidelines and Best Practices 19. Index 20. Other Books You May Enjoy

What is a shell?

Computer software known as a shell makes an operating system’s services accessible to users or other programs.

A shell is a program that receives commands and sends them to the operating system for processing, to put it simply. In an interactive session, the user has the option of typing commands from the keyboard, or they can be written in a shell script that can be reused. On a Unix-type system such as Linux in the past, it was the sole user interface (UI) accessible. Today, in addition to command-line interfaces (CLIs) such as shells, we also have graphical UIs (GUIs).

The fundamental capability of shells is the ability to launch command-line programs that are already installed on the system. They also offer built-ins and scripting control structures such as conditionals and loops. Each shell has its own way of doing that. Some shells still support the Bourne shell, one of the original shells that was created for an early Unix system by a programmer named Steve Bourne and later standardized in the Portable Operating System Interface (POSIX) standard. Other projects, such as csh/tcsh, zsh, and fish, purposefully utilize a different syntax.

In order to use command-line shells, a user must be knowledgeable about commands, their calling syntax, and the fundamentals of the shell’s specific scripting language.

A Linux user can utilize a variety of shells, including the following:

  • sh: A POSIX-compatible Bourne shell. In modern distros, it’s usually just Bourne again shell (Bash) running in compatibility mode.
  • csh/tcsh: These come from the Berkeley Software Distribution (BSD) Unix system family but are also available on Linux; their scripting syntax is similar to that of C, and they are incompatible with the Bourne shell.
  • ksh: A Bourne shell derivative that was once very popular.
  • bash: Bash is the most common Linux shell and was created for the GNU project.
  • zsh and fish: These are highly customizable and feature-rich shells that are intentionally different from sh derivatives and require learning, but have large communities of enthusiasts.

They all share similar properties, but each has its own unique attributes.

In this book, we will assume you are using Bash since it’s the default in most Linux distributions.

The Unix shell and Bash command language were both developed by Brian Fox for the GNU project. These were intended to serve as free software replacements for the Bourne shell. Since its introduction in 1989, it has remained the default login shell for the vast majority of Linux distributions. Linus Torvalds ported Bash and the GNU Compiler Collection (GCC) to Linux as one of the initial applications.

Bash has the following features:

  • The shell will check to see whether a command is built in before searching through a list of directories to locate the program if not. This set is known as the search path. By running the echo $PATH command in Bash, you can view it. The home directory and its subdirectory are included in the search path in addition to the current directory. You are able to create your own programs and call them up just by inputting their names. No matter which directory you are now in, a program such as this will be found and launched if it is stored in the bin directory. We will find out more about the Linux directory structure in Chapter 3, The Linux Filesystem.
  • As with other Linux programs, the shell has a current directory linked to it. When looking for files, Linux-based programs begin in the current directory. To move the current directory to another location in the Linux filesystem, use the cd shell command. The current working directory is typically visible in the command prompt of modern shells. To check the version of your shell, run the echo $SHELL command. You will get an output such as /bin/bash.
  • A command is executed by designating it. The majority of Linux commands are just programs that the shell runs. For instance, the following ls command scans the current directory and lists the names of its files: ls -la.
  • Commands frequently have argument strings that could, for example, be filenames. For instance, the following command switches to the tmp directory in your home directory. The shell interprets the tilde character as your home directory:
    cd ~/tmp
  • Multiple arguments are required for some commands. The copy command, for instance, requires two arguments: the file to copy and its destination. This is demonstrated as follows by copying file1 to a new file, file2:
    cp file1 file2
  • The flag or option argument strings for some commands typically start with -. The flags change how the invoked application behaves. When the following command is used, ls outputs a lengthy listing of files arranged by creation time:
    ls -lt
  • Wildcards will be expanded by the shell to match filenames in the current directory. For example, to display a directory listing of files named anything.sh, type the following:
    ls -l *.sh
  • Standard input (stdin) and standard output (stdout) are concepts that are followed by the majority of Linux commands and programs. The program receives a stream of data as stdin and produces a stream of output as stdout. These are frequently both connected to Terminal so that input is made via the keyboard and output is displayed on the screen. You can reroute stdin and stdout using the shell. cat is an abbreviation for concatenate. When run, the following command will show you the contents of one or more files without requiring you to open them for editing:
    cat /etc/passwd
  • The shell has the ability to pipe data from one program’s output to another’s input. | is the pipe symbol. To count the number of words in testfile.txt, we can concatenate the file and pass the output into the wc program, like so:
    cat testfile.txt | wc -w
    1198

Or, to count the number of lines from a testfile.txt file, we can use the following command:

cat testfile.txt | wc -l
289
  • You can create aliases for commands or groups of commands that you use frequently or find difficult to input. For instance, we could use the top10 alias to find the top 10 files in the current directory. head will show only the top lines. An alias is a shortcut for a command—for example, rather than remembering a very long command, you can create an alias that you can remember easily. Here’s an example:
    alias top10="du -hsx * | sort -rh | head -10"
  • Some variables are predefined, such as $HOME, which is your home directory. To see a list of assigned variables, type the following command:
    set
  • A manual (man) page is like a manual with instructions and descriptions about each command. Run the following command to view the man page for Bash:
    bash-3.2$ man bash
  • Scripts of shell commands can be written. These can be called just like compiled programs (that is, just by naming them). For instance, we first create a file in /bin containing the following in order to construct a script named top10.sh that displays the top 10 biggest files in the current directory:
    #! /bin/bash
    du -hsx * | sort -rh | head -10
  • We must next use the chmod command to make the file executable before we can run it normally:
    chmod +x ~/bin/top10.sh
    ./top10.sh

See the man page on bash for more details (type man bash).

The up arrow key on the keyboard in Bash’s extra mechanism enables you to access and modify past commands. The most recent command is displayed on Terminal again when you press the up arrow key. To access previous commands, press the up arrow key once more. Press Enter to run the command once more. Use the Delete key to remove characters from the command’s end, or the back arrow key to move the cursor and change the command’s contents by inserting or deleting characters.

By using the history command, you can view the history of commands.

You can rerun any command from the history by pressing ! and the line number—for example, !345.

Now that you know how to interact with the shell and what is happening when you type these commands, in the next section, we will try to practice some basic commands to make you more confident when you interact with Terminal.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image