Shell and Scripting

Back to Infrastructure Index

TLDR

In summary, PowerShell, Bash, and Zsh are different command-line shells, each with its own features and syntax. Scripts, such as shell scripts or Bash scripts, are files containing commands that can be executed by the respective shell. The terminal is the interface where you interact with the shell and run commands or execute scripts.

Shell Scripts Explained

What is a Shell Script?

  • A shell script is a computer program designed to be run by a Unix shell, which is a command-line interpreter
  • Shell scripts are used to:
    • Automate tasks
    • Combine multiple commands
    • Perform various operations
    • Manage system administration tasks

Bash (Bourne Again Shell)

  • Bash is a specific Unix shell and command language
  • It is an enhanced version of the original Bourne Shell (sh)
  • A Bash script is a shell script written specifically for the Bash shell
  • Uses Bash syntax and can utilize Bash-specific features and commands

Key Point: A Bash script is a specific type of shell script written for the Bash shell, while a shell script is a more general term that can refer to scripts written for any Unix shell.

Command-Line Shells

Overview

PowerShell, Bash, and Zsh are all different types of command-line shells used for interacting with an operating system.

PowerShell

Platform: Cross-platform (Windows, macOS, Linux)
Developer: Microsoft

Key Features:

  • Built on the .NET Framework
  • Provides both command-line shell and scripting language
  • Object-oriented programming support
  • Advanced pipeline processing
  • Extensive cmdlets for system administration and automation

Use Cases:

  • Windows system administration
  • Automation and configuration management
  • Task automation across platforms

Example:

# PowerShell example
Get-Process | Where-Object {$_.CPU -gt 100} | Select-Object Name, CPU

Bash (Bourne Again Shell)

Platform: Unix, Linux, macOS (default on many systems)
Type: Unix shell and command language

Key Features:

  • Widely used as the default shell on many Linux distributions and macOS
  • Command-line interface for interacting with the operating system
  • Running commands and executing scripts
  • Command-line editing and command history
  • Variable expansion and control structures for scripting

Use Cases:

  • Linux/Unix system administration
  • DevOps automation
  • CI/CD pipelines
  • Script automation

Example:

#!/bin/bash
# Bash example
for file in *.txt; do
    echo "Processing $file"
    cat "$file" | grep "pattern"
done

Zsh (Z Shell)

Platform: Unix, Linux, macOS
Type: Extended Bourne Shell (sh)

Key Features:

  • Highly customizable
  • Advanced command-line completion
  • Spelling correction
  • Plugin support (Oh My Zsh framework)
  • Compatible with Bash syntax
  • More interactive and user-friendly experience

Compatibility:

  • Most Bash scripts can run on Zsh without modification
  • Backwards compatible with sh

Use Cases:

  • Interactive shell usage
  • Development environments
  • Enhanced productivity with plugins
  • Custom prompt configurations

Example:

# Zsh with advanced features
autoload -U colors && colors
echo "$fg[red]Error: $reset_color Something went wrong"

Shell Comparison

FeaturePowerShellBashZsh
PlatformCross-platformUnix/Linux/macOSUnix/Linux/macOS
Object-oriented✅ Yes❌ No❌ No
Auto-completionGoodBasicExcellent
CustomizationGoodGoodExcellent
Plugin ecosystemModerateModerateExtensive
Learning curveSteepModerateModerate
Scripting styleCmdlet-basedCommand-basedCommand-based

Common Shell Operations

File Operations

# List files
ls -la
 
# Copy files
cp source.txt destination.txt
 
# Move/Rename
mv oldname.txt newname.txt
 
# Remove files
rm file.txt

Process Management

# List running processes
ps aux
 
# Kill a process
kill -9 <pid>
 
# Background process
command &
 
# Check running jobs
jobs

Text Processing

# Search in files
grep "pattern" file.txt
 
# Stream editor
sed 's/old/new/g' file.txt
 
# Text processor
awk '{print $1}' file.txt
 
# View file contents
cat file.txt
less file.txt

Variables and Environment

# Set variable
MY_VAR="value"
 
# Use variable
echo $MY_VAR
 
# Environment variable
export PATH=$PATH:/new/path
 
# View environment
env
printenv

Shell Script Best Practices

1. Use Shebang

#!/bin/bash
# or
#!/usr/bin/env bash

2. Set Error Handling

set -e  # Exit on error
set -u  # Exit on undefined variable
set -o pipefail  # Exit on pipe failure

3. Add Comments

# This script does something important
# Usage: ./script.sh [options]

4. Use Functions

function backup_files() {
    local source=$1
    local dest=$2
    cp -r "$source" "$dest"
}

5. Quote Variables

# Good
echo "$MY_VAR"
cp "$source" "$destination"
 
# Bad
echo $MY_VAR
cp $source $destination

6. Check Command Success

if command -v docker &> /dev/null; then
    echo "Docker is installed"
else
    echo "Docker is not installed"
fi

Terminal vs Shell

Terminal

  • The interface where you interact with the shell
  • Examples: GNOME Terminal, iTerm2, Windows Terminal, Alacritty
  • Provides the window for shell interaction

Shell

  • The interpreter that executes commands
  • Examples: Bash, Zsh, Fish, PowerShell
  • Processes your commands and scripts

Relationship: Terminal displays the output from the shell; shell interprets and executes your commands.


Related: