Skip to main content

shell

Shell User Manual

This guide provides a detailed explanation of basic and advanced techniques for using the Bash shell. Additionally, it covers some features of Zsh. A shell is a program used to operate the command-line interface, commonly used for system administration and scripting.

Table of Contents

Shell Basics

Starting the Shell

Open a terminal (or command prompt) to start the shell. By default, Bash is usually started.

bash

Basic Commands

Below are frequently used basic commands in Bash.

  • ls: Displays the contents of a directory.
    ls
  • cd: Changes the current directory.
    cd /path/to/directory
  • pwd: Prints the current directory.
    pwd
  • echo: Outputs text.
    echo "Hello, World!"
  • cat: Displays the contents of a file.
    cat file.txt

Bash Basics

Using Variables

Learn how to define and use variables in Bash.

Defining and Using Variables

name="Alice"
echo $name

Environment Variables

export PATH=$PATH:/new/path

Control Structures

Control structures in Bash include if statements, for loops, and while loops.

If Statements

if [ condition ]; then
echo "Condition is true"
else
echo "Condition is false"
fi

For Loops

for i in 1 2 3; do
echo "Number $i"
done

While Loops

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

Defining Functions

Learn how to define and use functions in Bash.

my_function() {
echo "Hello, $1"
}

my_function "Alice"

Advanced Bash Usage

Regular Expressions and Pattern Matching

Using regular expressions and pattern matching in Bash.

if [[ "hello" =~ ^h.*o$ ]]; then
echo "Matches"
fi

Creating Shell Scripts

How to create and execute shell scripts.

#!/bin/bash
echo "This is a shell script"

Give the script execute permission and run it.

chmod +x script.sh
./script.sh

Job Management

Manage jobs in Bash.

Background Jobs

sleep 10 &

Bringing Jobs to Foreground

fg %1

Input/Output Redirection

Learn how to use input/output redirection in Bash.

Redirecting Standard Output

echo "Hello, World!" > output.txt

Redirecting Standard Error

ls /nonexistent 2> error.txt

Redirecting Both Standard Output and Error

command > output.txt 2>&1

Introduction to Zsh

Installing Zsh

How to install Zsh.

On macOS

brew install zsh

On Ubuntu

sudo apt-get install zsh

Basic and Advanced Features

An overview of Zsh’s basic and extended functionalities.

Autocompletion

How to enable Zsh’s powerful autocompletion.

autoload -Uz compinit
compinit

Using Oh My Zsh

Customize Zsh using Oh My Zsh.

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Advanced Shell Operations

Process Substitution

Using process substitution.

diff <(ls dir1) <(ls dir2)

Aliases and Shell Customization

Create aliases and customize the shell in Bash or Zsh.

Creating Aliases

alias ll='ls -la'

Customizing the Shell Prompt

export PS1="\u@\h \W> "

Command History Manipulation

How to work with command history in Bash or Zsh.

Viewing History

history

Re-running Specific Commands

!42

Searching History

Ctrl + r

This concludes the explanation of basic and advanced shell usage in Bash and Zsh. Shells are powerful and flexible tools, and understanding their features is key to using them efficiently.