Table of Contents
Learn Linux - Part 3
Introduction to Shell

Getting the hang of the Linux filesystem can be a bit tricky at first, but it’s a super handy skill to have in your toolkit. I put together this guide mainly for myself—something I can refer back to whenever I need a refresher. But if it helps someone else along the way, that’s a bonus!

Introduction

The shell is a vital component of the Linux operating system, acting as the interface between the user and the kernel. It allows users to execute commands, run scripts, and automate tasks. The shell interprets user input and translates it into actions performed by the system. There are various types of shells available in Linux, such as Bash (Bourne Again Shell), Zsh, and Fish. Among these, Bash is the most widely used and is considered the best shell for system administrators due to its robust features and extensive scripting capabilities.

Purpose of the Shell

The primary purpose of the shell is to provide an environment where users can interact with the operating system. It facilitates executing commands, running programs, and manipulating files and directories. The shell also supports scripting, enabling users to automate repetitive tasks, schedule jobs, and create complex workflows. When a user types a command in the terminal, the shell interprets it and communicates with the kernel to perform the requested action.

Introducing Bash

Bash, short for Bourne Again Shell, is an enhanced version of the original Unix shell (sh). It is the default shell on most Linux distributions, including Ubuntu. Bash provides powerful features like command history, tab completion, and scripting capabilities, making it an excellent choice for system administrators.

To start using Bash, you open a terminal window. In most Linux distributions, Bash is the default shell, so you don’t need to do anything special to start it. You can check the current shell with the following command:

				
					echo $SHELL
/bin/bash
				
			

This command outputs the path to the current shell, confirming that you are using Bash.

Basic Navigation Commands

Navigating Directories

Use the cd command to change directories:

				
					cd /projects/AlphaProject
				
			

To confirm your location, use pwd:

				
					pwd
/projects/AlphaProject
				
			

Listing Directory Contents

The ls command lists the contents of a directory:

				
					ls
developers designers qa
				
			

Add -l for a detailed listing:

				
					ls -l
total 12
drwxr-xr-x 2 user user 4096 Jan 1 12:00 developers
drwxr-xr-x 2 user user 4096 Jan 1 12:00 designers
drwxr-xr-x 2 user user 4096 Jan 1 12:00 qa
				
			

Creating and Deleting Files

Use the touch command to create an empty file and rm to delete it:

				
					touch testfile.txt
ls
developers designers qa testfile.txt
rm testfile.txt
				
			

Copying and Moving Files

The cp command copies files, and mv moves or renames them:

				
					cp /projects/AlphaProject/designers/assets/logo.png /projects/AlphaProject/qa/
mv /projects/AlphaProject/qa/logo.png /projects/AlphaProject/qa/logo_backup.png
				
			

Using Command History

One of the convenient features of Bash is command history. Bash keeps a record of previously executed commands, which you can access using the up and down arrow keys. This feature saves time and reduces errors by allowing you to quickly repeat or modify previous commands.

To view the command history, use:

				
					history
				
			

This command lists all previously executed commands with their respective numbers. You can rerun a command by typing ! followed by the command number:

				
					!10
				
			

Tab Completion

Bash supports tab completion, which speeds up typing and reduces errors. When you start typing a command, file name, or directory name, pressing the Tab key will auto-complete the text or show possible completions. For example:

				
					cd /projects/AlphaProject/developers/sc
				
			

If there is only one directory that starts with sc, Bash will auto-complete it. If there are multiple matches, pressing Tab again will display all possibilities.

Bash Variables

Bash allows you to create and use variables to store data. Variables can hold text, numbers, or command output. To create a variable, simply assign a value to it:

				
					PROJECT_DIR=/projects/AlphaProject
				
			

To use the variable, prefix it with a dollar sign:

				
					cd $PROJECT_DIR
pwd
/projects/AlphaProject
				
			

Variables are useful for storing paths, filenames, and other data you frequently use in your scripts or commands.

Bash Scripting

Bash scripting enables you to automate tasks by writing scripts that execute a series of commands. A Bash script is simply a text file with a .sh extension that contains Bash commands.

Creating a Basic Script

Open a text editor and enter the following lines:

				
					#!/bin/bash
echo "Hello, AlphaProject!"
				
			

Save the file as hello.sh.

Make the script executable:

				
					chmod +x hello.sh
				
			

Run the script:

				
					./hello.sh
Hello, AlphaProject!
				
			

The #!/bin/bash line at the top is called a shebang and indicates that the script should be run using Bash. The echo command prints the text to the terminal.

Conditional Statements

Bash scripts can include conditional statements to perform different actions based on conditions. The if statement is used to test conditions:

				
					#!/bin/bash
if [ -d "/projects/AlphaProject" ]; then
  echo "AlphaProject directory exists."
else
  echo "AlphaProject directory does not exist."
fi
				
			

Save this script as check_dir.sh, make it executable, and run it:

				
					chmod +x check_dir.sh
./check_dir.sh
AlphaProject directory exists.
				
			

The -d option checks if the specified path is a directory.

Loops in Bash

Loops allow you to execute a set of commands multiple times.

For Loop

The for loop iterates over a list of items:

				
					#!/bin/bash
for team in developers designers qa; do
  echo "Setting up $team directory..."
  mkdir -p /projects/AlphaProject/$team
done
				
			

Save this script as setup_teams.sh, make it executable, and run it:

				
					chmod +x setup_teams.sh
./setup_teams.sh
Setting up developers directory...
Setting up designers directory...
Setting up qa directory...
				
			

This script creates directories for each team in the AlphaProject.

While Loops

The while loop runs as long as a condition is true:

				
					#!/bin/bash
count=1
while [ $count -le 5 ]; do
  echo "Count: $count"
  ((count++))
done
				
			

Save this script as count.sh, make it executable, and run it:

				
					chmod +x count.sh
./count.sh
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
				
			

This script counts from 1 to 5, demonstrating the use of a while loop.

Functions in Bash

Functions in Bash allow you to group commands and reuse them. Define a function by using the function keyword followed by the function name and curly braces:

				
					#!/bin/bash
function greet {
  echo "Hello, $1!"
}
greet AlphaProject
				
			

Save this script as greet.sh, make it executable, and run it:

				
					chmod +x greet.sh
./greet.sh
Hello, AlphaProject!
				
			

This script defines a greet function that takes an argument and prints a greeting message.

These skills are essential for system administrators, forming the basis for more advanced topics covered in future sections. As we continue with the AlphaProject use-case, you’ll discover the practical applications of Bash’s capabilities, boosting your skills and confidence in administering Linux systems.

Leave a Comment