In this tutorial, we'll learn bash basics commands and syntax.
Bash (Bourne Again SHell) is the default command-line interface for most Linux distributions and an essential tool for system administrators, developers, and anyone working with Linux. Understanding its syntax and basic commands is the first step in mastering Bash scripting. This tutorial will provide a thorough explanation of Bash syntax and demonstrate how to use basic Linux commands in scripts.
Prerequisites
Before getting started, ensure you have the following:
- A KVM VPS or dedicated server with any Linux distro installed.
- A non-root user with sudo privileges.
- Basic knowledge of Linux commands.
Bash Basics Commands and Syntax
Understanding Bash Syntax
Bash syntax is relatively straightforward but has unique characteristics that require attention. Let’s break it down:
1. Shebang (#!)
Every Bash script begins with a shebang (#!) followed by the path to the interpreter:
#!/bin/bash
The shebang informs the system that the file should be executed with Bash.
2. Comments
Use the # symbol for comments. Comments are ignored by the interpreter and help explain the script:
# This is a comment
3. Commands
Commands in Bash follow this general syntax:
command [options] [arguments]
- command: The program or command to execute (e.g., ls, echo).
- options: Modifiers for the command (e.g., -l, -a).
- arguments: Inputs for the command (e.g., filenames or directories).
4. Script Execution
Make a script executable using the chmod command and then run it:
chmod +x script.sh
./script.sh
Alternatively, execute a script with Bash directly:
bash script.sh
5. Line Breaks and Continuation
Commands typically end with a newline. For long commands, use a backslash (\
) to continue on the next line:
echo "This is a long line that will continue \
on the next line."
6. Quoting
Quoting controls how strings and variables are interpreted:
- Double quotes ("): Allow variable and command substitution.
- Single quotes ('): Treat everything literally.
- Backticks (`command`) or $(command): Execute commands.
Example:
name="John"
echo "Hello, $name"
echo 'Hello, $name'
echo "Current directory: $(pwd)"
Using Basic Linux Commands in Scripts
Bash scripts often incorporate fundamental Linux commands. Below are examples of commonly used commands and their application in scripts.
1. echo: Print Text
The echo command displays text or variables:
#!/bin/bash
name="Alice"
echo "Hello, $name!"
2. ls: List Directory Contents
The ls command lists files and directories:
#!/bin/bash
echo "Files in current directory:"
ls -l
3. cd: Change Directory
The cd command navigates directories:
#!/bin/bash
cd /tmp
echo "Now in: $(pwd)"
4. mkdir and rmdir: Create and Remove Directories
Create a directory with mkdir and remove it with rmdir:
#!/bin/bash
mkdir my_directory
echo "Directory created: my_directory"
rmdir my_directory
echo "Directory removed: my_directory"
5. touch: Create an Empty File
Use touch to create files:
#!/bin/bash
touch file1.txt
echo "Created file: file1.txt"
6. cat: Display File Content
Display file contents using cat:
#!/bin/bash
echo "Hello, World!" > example.txt
cat example.txt
7. ifconfig or ip: Network Information
Retrieve network details (requires root permissions for some options):
#!/bin/bash
ip a
8. df and du: Disk Usage
Monitor disk usage:
#!/bin/bash
echo "Disk usage:"
df -h
echo "Directory usage for /home:"
du -sh /home
9. grep: Search for Patterns
Search text with grep:
#!/bin/bash
echo "Sample text file" > sample.txt
grep "Sample" sample.txt
10. Combining Commands
Chain commands using ;
, &&
, or ||
:
#!/bin/bash
echo "Creating directory..." && mkdir my_folder || echo "Failed to create directory."
Scripting Example: A Simple Backup Script
Below is a script that demonstrates basic syntax and commands:
#!/bin/bash
# Variables
backup_dir="/backup"
src_dir="/home/user/documents"
date=$(date +%Y-%m-%d)
# Create backup directory if it doesn't exist
if [ ! -d "$backup_dir" ]; then
echo "Creating backup directory: $backup_dir"
mkdir -p $backup_dir
fi
# Perform backup
backup_file="$backup_dir/backup-$date.tar.gz"
echo "Backing up $src_dir to $backup_file"
tar -czf $backup_file $src_dir
# Verify backup
if [ -f "$backup_file" ]; then
echo "Backup successful: $backup_file"
else
echo "Backup failed."
fi
With a solid grasp of Bash syntax and basic commands, you are now equipped to start writing simple yet effective scripts. In the next installment of this series, we’ll dive into Variables and Data Types in Bash, exploring how to store and manipulate data effectively.