DEV Community

Zeshan
Zeshan

Posted on

Bash Scripting Fundamentals

1. Introduction to Bash

1.1 What is Bash?

Bash (Bourne Again Shell) is a command processor that typically runs in a text window where the user types commands that cause actions.

Image description
Bash can also read and execute commands from a file, called a script.

1.2 Creating and Running a Script

  • Creating a script: Use a text editor to create a file with the .sh extension.
 nano script.sh 
Enter fullscreen mode Exit fullscreen mode
  • Making the script executable:
 chmod +x script.sh 
Enter fullscreen mode Exit fullscreen mode
  • Running the script:
 ./script.sh 
Enter fullscreen mode Exit fullscreen mode

2. Basic Commands

2.1 Echo

  • Description: Prints text to the terminal.
 echo "Hello, World!" 
Enter fullscreen mode Exit fullscreen mode

2.2 Variables

  • Description: Storing and using data.
 NAME="John" echo $NAME 
Enter fullscreen mode Exit fullscreen mode

2.3 Comments

  • Description: Adding comments to a script for readability.
 # This is a comment echo "This is a script" 
Enter fullscreen mode Exit fullscreen mode

3. Input and Output

3.1 Reading User Input

  • Description: Taking input from the user.
 read "Enter your name: " NAME echo "Hello, $NAME" 
Enter fullscreen mode Exit fullscreen mode

3.2 Redirecting Output

  • Description: Redirecting command output to a file.
 echo "This is a test" > file.txt 
Enter fullscreen mode Exit fullscreen mode

3.3 Appending Output

  • Description: Appending command output to a file.
 echo "This is a test" >> file.txt 
Enter fullscreen mode Exit fullscreen mode

3.4 Redirecting Input

  • Description: Using a file as input to a command.
 wc -l < file.txt 
Enter fullscreen mode Exit fullscreen mode

3.5 Piping

  • Description: Using the output of one command as input to another.
 cat file.txt | grep "test" 
Enter fullscreen mode Exit fullscreen mode

4. Control Structures

4.1 Conditional Statements

  • Description: Making decisions in scripts.
 if [ "$NAME" == "John" ]; then echo "Hello, John!" else echo "You are not John." fi 
Enter fullscreen mode Exit fullscreen mode

4.2 Loops

  • Description: Repeating a set of commands.

4.2.1 For Loop

 for i in {1..5}; do echo "Welcome $i" done 
Enter fullscreen mode Exit fullscreen mode

4.2.2 While Loop

 COUNTER=0 while [ $COUNTER -lt 5 ]; do echo "Counter: $COUNTER" COUNTER=$((COUNTER + 1)) done 
Enter fullscreen mode Exit fullscreen mode

4.2.3 Until Loop

 COUNTER=0 until [ $COUNTER -ge 5 ]; do echo "Counter: $COUNTER" COUNTER=$((COUNTER + 1)) done 
Enter fullscreen mode Exit fullscreen mode

5. Functions

5.1 Defining and Calling Functions

  • Description: Grouping commands into a reusable block.
 my_function() { echo "Hello from my_function" } my_function 
Enter fullscreen mode Exit fullscreen mode

6. Data Structures

6.1 Arrays

  • Description: Using arrays to store multiple values.
 NAMES=("John" "Jane" "Doe") echo "First Name: ${NAMES[0]}" 
Enter fullscreen mode Exit fullscreen mode

6.2 Tuples (Simulated with Arrays)

  • Description: Using arrays to simulate tuples.
 TUPLE=("Alice" 25 "Developer") echo "Name: ${TUPLE[0]}, Age: ${TUPLE[1]}, Job: ${TUPLE[2]}" 
Enter fullscreen mode Exit fullscreen mode

6.3 Sets (Simulated with Arrays and Associative Arrays)

  • Description: Using arrays and associative arrays to simulate sets.
 declare -A SET SET["Apple"]=1 SET["Banana"]=1 if [[ ${SET["Apple"]} ]]; then echo "Apple is in the set." fi 
Enter fullscreen mode Exit fullscreen mode

7. Operators

7.1 Mathematical Operators

  • Description: Performing arithmetic operations.
 A=5 B=3 SUM=$((A + B)) echo "Sum: $SUM" 
Enter fullscreen mode Exit fullscreen mode

7.2 Logical Operators

  • Description: Using logical operators in conditions.
 if [ $A -eq 5 ] && [ $B -eq 3 ]; then echo "Both conditions are true." fi 
Enter fullscreen mode Exit fullscreen mode

7.3 Semantic Operators

  • Description: Using comparison operators.
 if [ "$A" -eq 5 ]; then echo "A is 5" fi if [ "$A" -ne 5 ]; then echo "A is not 5" fi 
Enter fullscreen mode Exit fullscreen mode

8. Type Casting

  • Description: Type casting in Bash is generally about ensuring variable content is treated correctly.
 VAR="123" NUM=$((VAR + 0)) # Cast string to number for arithmetic echo $NUM 
Enter fullscreen mode Exit fullscreen mode

9. API Calls

  • Description: Making HTTP requests using tools like curl.
 RESPONSE=$(curl -s -X GET "https://api.example.com/data") echo "Response: $RESPONSE" 
Enter fullscreen mode Exit fullscreen mode

10. Automating Tasks

10.1 Cron Jobs

  • Description: Scheduling scripts to run automatically.
 # Edit the crontab file crontab -e # Add a job to run every day at midnight 0 0 * * * /path/to/script.sh 
Enter fullscreen mode Exit fullscreen mode

10.2 Using at

  • Description: Scheduling a one-time task.
 echo "/path/to/script.sh" | at now + 5 minutes 
Enter fullscreen mode Exit fullscreen mode

10.3 Background Jobs

  • Description: Running scripts in the background.
 ./script.sh & 
Enter fullscreen mode Exit fullscreen mode

11. Advanced Topics

11.1 Using sed and awk

  • Description: Text processing with sed and awk.

11.1.1 sed

 sed 's/old/new/g' file.txt 
Enter fullscreen mode Exit fullscreen mode

11.1.2 awk

 awk '{print $1}' file.txt 
Enter fullscreen mode Exit fullscreen mode

11.2 Traps

  • Description: Capturing signals.
 trap "echo 'Script interrupted'; exit" SIGINT while true; do echo "Running..." sleep 1 done 
Enter fullscreen mode Exit fullscreen mode

12. Debugging

12.1 Enabling Debugging

  • Description: Running a script in debug mode.
 bash -x script.sh 
Enter fullscreen mode Exit fullscreen mode

12.2 Using set

  • Description: Setting options for debugging.
 set -x # Enable debugging set +x # Disable debugging 
Enter fullscreen mode Exit fullscreen mode

12.3 Error Handling

  • Description: Handling errors in scripts.
 command || { echo "Command failed"; exit 1; } 
Enter fullscreen mode Exit fullscreen mode

12.4 Using trap for Cleanup

  • Description: Cleaning up resources on script exit.
 cleanup() { echo "Cleaning up..." # perform cleanup here } trap cleanup EXIT 
Enter fullscreen mode Exit fullscreen mode

12.5 Logging

  • Description: Creating log files for your scripts.
 LOG_FILE="/var/log/script.log" echo "Script started" >> $LOG_FILE 
Enter fullscreen mode Exit fullscreen mode

13. More on Arrays

13.1 Array Operations

  • Description: Array length and iteration.
 ARR=("one" "two" "three") echo "Array length: ${#ARR[@]}" for ITEM in "${ARR[@]}"; do echo $ITEM done 
Enter fullscreen mode Exit fullscreen mode

13.2 Associative Arrays

  • Description: Using associative arrays.
 declare -A ASSOC_ARRAY ASSOC_ARRAY["name"]="John" ASSOC_ARRAY["age"]="30" echo "Name: ${ASSOC_ARRAY["name"]}" echo "Age: ${ASSOC_ARRAY["age"]}" 
Enter fullscreen mode Exit fullscreen mode

13.3 Multidimensional Arrays

  • Description: Simulating multidimensional arrays.
 ARRAY_2D[0]="1 2 3" ARRAY_2D[1]="4 5 6" ARRAY_2D[2]=" 7 8 9" for ROW in "${ARRAY_2D[@]}"; do for ITEM in $ROW; do echo -n "$ITEM " done echo done 
Enter fullscreen mode Exit fullscreen mode

14. String Operations

14.1 Substring Extraction

  • Description: Extracting a substring from a string.
 STRING="Hello World" echo ${STRING:6:5} # Outputs "World" 
Enter fullscreen mode Exit fullscreen mode

14.2 String Length

  • Description: Getting the length of a string.
 echo ${#STRING} # Outputs "11" 
Enter fullscreen mode Exit fullscreen mode

14.3 String Replacement

  • Description: Replacing part of a string.
 echo ${STRING/World/Bash} # Outputs "Hello Bash" 
Enter fullscreen mode Exit fullscreen mode

15. File Operations

15.1 File Tests

  • Description: Testing properties of files.
 if [ -f "file.txt" ]; then echo "file.txt is a regular file." fi if [ -d "directory" ]; then echo "directory is a directory." fi 
Enter fullscreen mode Exit fullscreen mode

15.2 Reading Files

  • Description: Reading a file line by line.
 while IFS= read -r line; do echo "$line" done < file.txt 
Enter fullscreen mode Exit fullscreen mode

15.3 Writing to Files

  • Description: Writing to a file.
 echo "Hello, World!" > file.txt 
Enter fullscreen mode Exit fullscreen mode

15.4 Appending to Files

  • Description: Appending to a file.
 echo "This is a new line" >> file.txt 
Enter fullscreen mode Exit fullscreen mode

16. Network Operations

16.1 Downloading Files

  • Description: Using wget or curl to download files.
 wget http://example.com/file.zip curl -O http://example.com/file.zip 
Enter fullscreen mode Exit fullscreen mode

16.2 Uploading Files

  • Description: Using curl to upload files.
 curl -F "file=@/path/to/file" http://example.com/upload 
Enter fullscreen mode Exit fullscreen mode

16.3 Checking Internet Connection

  • Description: Pinging a server to check for internet connectivity.
 ping -c 4 google.com 
Enter fullscreen mode Exit fullscreen mode

17. Handling Large Data

17.1 Sorting and Uniqueness

  • Description: Sorting data and removing duplicates.
 sort file.txt | uniq > sorted.txt 
Enter fullscreen mode Exit fullscreen mode

17.2 Finding Patterns

  • Description: Using grep to find patterns.
 grep "pattern" file.txt 
Enter fullscreen mode Exit fullscreen mode

17.3 Splitting Files

  • Description: Splitting large files into smaller parts.
 split -l 1000 largefile.txt smallfile_ 
Enter fullscreen mode Exit fullscreen mode

18. Process Management

18.1 Checking Running Processes

  • Description: Using ps to list running processes.
 ps aux 
Enter fullscreen mode Exit fullscreen mode

18.2 Killing Processes

  • Description: Using kill to terminate processes.
 kill -9 PID 
Enter fullscreen mode Exit fullscreen mode

18.3 Monitoring System Resources

  • Description: Using top or htop to monitor system resources.
 top htop # Requires installation 
Enter fullscreen mode Exit fullscreen mode

19. Packaging and Distribution

19.1 Creating Tar Archives

  • Description: Archiving files using tar.
 tar -cvf archive.tar file1 file2 directory/ 
Enter fullscreen mode Exit fullscreen mode

19.2 Extracting Tar Archives

  • Description: Extracting files from a tar archive.
 tar -xvf archive.tar 
Enter fullscreen mode Exit fullscreen mode

19.3 Compressing Files

  • Description: Compressing files using gzip.
 gzip file.txt 
Enter fullscreen mode Exit fullscreen mode

19.4 Decompressing Files

  • Description: Decompressing files using gunzip.
 gunzip file.txt.gz 
Enter fullscreen mode Exit fullscreen mode

This extensive guide covers fundamental and advanced topics in Bash scripting

Top comments (7)

Collapse
 
moopet profile image
Ben Sinclair

In 4.1, you use a double-equals for equality:

if [ "$NAME" == "John" ]; then 
Enter fullscreen mode Exit fullscreen mode

and while this works in bash, it's not POSIX and won't work in most other shells. I'd recommend sticking to a single equals sign for equality:

if [ "$NAME" = "John" ]; then 
Enter fullscreen mode Exit fullscreen mode

In 18.2 you mention "Using kill to terminate processes" by using kill -9. That's might be slightly confusing since SIGKILL is 9 and SIGTERM is 15.

I'd steer clear of part 13 and similar altogether. If you're at the point where you're using "advanced arrays" in the shell, you almost certainly want to be using a dedicated scripting language instead.

Collapse
 
bobbyiliev profile image
Bobby

Great post ๐Ÿ‘ Well done!

In case that anyone wants to learn more, here is a free eBook:

GitHub logo bobbyiliev / introduction-to-bash-scripting

Free Introduction to Bash Scripting eBook

๐Ÿ’ก Introduction to Bash Scripting

This is an open-source introduction to Bash scripting guide/ebook that will help you learn the basics of Bash scripting and start writing awesome Bash scripts that will help you automate your daily SysOps, DevOps, and Dev tasks. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Bash scripts to combine different Linux commands and automate boring and repetitive daily tasks, so that you can focus on more productive and fun things.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Bash scripting.

๐Ÿš€ Download

To download a copy of the ebook use one of the following links:

๐Ÿ“˜ Chapters

The first 13 chapters would be purely focused on getting some solid Bash scripting foundations then the rest ofโ€ฆ

Collapse
 
litlyx profile image
Antonio | CEO at Litlyx.com

Great article! Your guide on Bash scripting fundamentals is an excellent resource for beginners. This tutorial is a great starting point.
Keep up the good work.

Sharing love from Italy.

Antonio, CEO at Litlyx

Collapse
 
elsyng profile image
Ellis • Edited

Very nice summary, easy reading, without clutter๐Ÿ‘

"Debugging": I think "tracing" would be a better term.

"help", "man", "type" (all 3 together) might be nice to add, so one can go deeper into any command. ("type" is very useful, to tell a file, command, builtin, function etc.)

Collapse
 
ankurloriya profile image
Ankur Loriya

First of all thanks for creating this page this is really helpful handbook.

Update section
5.1: (Function)
How to pass arguments ?
How to function handle arguments?

Create IF condition complete handbook in which contains

  • File exist check
  • File extension check
  • Boolean check
  • String compare with number
  • Float/fraction number check
  • AND and OR operator use
  • Convert to not operation
Collapse
 
ddebajyati profile image
Debajyati Dey

Informative

Collapse
 
bdmorin profile image
Brian

Why is the header 3 characters short of a bash bomb? Not cool in a beginner tute.