TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: 20 Bash Script Code Challenges for Beginners with Their Solutions
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Linux Tutorials

20 Bash Script Code Challenges for Beginners with Their Solutions

Last updated: Feb 24, 2024 11:06 pm
By Meenakshi Agarwal
Share
6 Min Read
Bash script code challenges
SHARE

The tutorial on Bash script code challenges for beginners aims to strengthen your understanding of Bash scripting. These challenges cover a range of topics, from basic script creation to file manipulation, enabling learners to grasp fundamental concepts in a practical context. Whether you are new to bash or want to improve your scripting skills, this tutorial is what you need. Go through these one by one and try to solve them by yourself for effective results.

Contents
Problem 1:Problem 2:Problem 3:Problem 4:Problem 5:Problem 6:Problem 7:Problem 8:Problem 9:Problem 10:Problem 11:Problem 12:Problem 13:Problem 14:Problem 15:Problem 16:Problem 17:Problem 18:Problem 19:Problem 20:

Also Read: 20 Shell Scripting Questions Answers for Practice

Bash Script Code Challenges for Beginners

Here are 20 Bash script code challenges for beginners with their solutions:

Problem 1:

Description: Write a Bash script that prints “Hello, World!” to the terminal.

Solution:

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

Problem 2:

Description: Create a script that takes two numbers as command-line arguments and prints their sum.

Solution:

#!/bin/bash
sum=$(( $1 + $2 ))
echo "Sum: $sum"

Problem 3:

Description: Write a script that checks if a file exists in the current directory. If it does, print “File exists,” otherwise, print “File not found.”

Solution:

#!/bin/bash
filename="example.txt"
if [ -e "$filename" ]; then
  echo "File exists"
else
  echo "File not found"
fi

Problem 4:

Description: Create a script that lists all files in the current directory.

Solution:

#!/bin/bash
for file in *; do
  echo "$file"
done

Problem 5:

Description: Write a script that prompts the user for their name and prints a personalized greeting.

Solution:

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"

Why not attempt this: Unix Shell Script Quiz for Beginners

Problem 6:

Description: Create a script that renames all files in the current directory with a “.bak” extension.

Solution:

#!/bin/bash
for file in *; do
  mv "$file" "$file.bak"
done

Problem 7:

Description: Write a script that counts the number of lines in a file.

Solution:

#!/bin/bash
file="example.txt"
line_count=$(wc -l < "$file")
echo "Number of lines: $line_count"

Problem 8:

Description: Create a script that calculates the factorial of a number provided as a command-line argument.

Solution:

#!/bin/bash
factorial=1
for ((i=1; i<=$1; i++)); do
  factorial=$((factorial * i))
done
echo "Factorial of $1 is: $factorial"

Problem 9:

Description: Write a script that finds and prints all even numbers between 1 and 20.

Solution:

#!/bin/bash
for ((i=2; i<=20; i+=2)); do
  echo "$i"
done

Run shell in your browser, check this: Chrome Python Shell Extensions

Problem 10:

Description: Create a script that searches for a specific string in all text files in the current directory.

Solution:

#!/bin/bash
search_string="example"
for file in *.txt; do
  grep -q "$search_string" "$file" && echo "Found in $file"
done

Problem 11:

Description: Write a script that takes a number as input and prints whether it is even or odd.

Solution:

#!/bin/bash
echo "Enter a number:"
read number
if [ $((number % 2)) -eq 0 ]; then
  echo "Even"
else
  echo "Odd"
fi

Problem 12:

Description: Create a script that prints the current date and time.

Solution:

#!/bin/bash
current_datetime=$(date +"%Y-%m-%d %H:%M:%S")
echo "Current date and time: $current_datetime"

Problem 13:

Description: Write a script that calculates the sum of all numbers from 1 to 100.

Solution:

#!/bin/bash
sum=0
for ((i=1; i<=100; i++)); do
  sum=$((sum + i))
done
echo "Sum of numbers from 1 to 100: $sum"

Problem 14:

Description: Create a script that checks if a given string is a palindrome.

Solution:

#!/bin/bash
echo "Enter a string:"
read input_string
reverse_string=$(echo "$input_string" | rev)
if [ "$input_string" == "$reverse_string" ]; then
  echo "Palindrome"
else
  echo "Not a palindrome"
fi

Problem 15:

Description: Write a script that prints the multiplication table of a given number.

Solution:

#!/bin/bash
echo "Enter a number:"
read number
for ((i=1; i<=10; i++)); do
  result=$((number * i))
  echo "$number x $i = $result"
done

Another quiz for you: Shell Script Quiz for Unix/Linux Geeks

Problem 16:

Description: Create a script that calculates the average of a list of numbers provided as command-line arguments.

Solution:

#!/bin/bash
sum=0
for number in "$@"; do
  sum=$((sum + number))
done
average=$((sum / $#))
echo "Average: $average"

Problem 17:

Description: Write a script that displays the contents of a file in reverse order.

Solution:

#!/bin/bash
file="example.txt"
tac "$file"

Problem 18:

Description: Create a script that removes all empty files in the current directory.

Solution:

#!/bin/bash
find . -type f -empty -delete

Problem 19:

Description: Write a script that finds and prints all prime numbers between 1 and 50.

Solution:

#!/bin/bash
for ((i=2; i<=50; i++)); do
  is_prime=true
  for ((j=2; j<i; j++)); do
    if [ $((i % j)) -eq 0 ]; then
      is_prime=false
      break
    fi
  done
  if [ "$is_prime" == true ]; then
    echo "$i"
  fi
done

Problem 20:

Description: Create a script that sorts the lines of a file in alphabetical order.

Solution:

#!/bin/bash
file="example.txt"
sort "$file"

Don’t miss this: Basic Linux Questions Freshers

In conclusion, these Bash script challenges are more than just code snippets; they are gateways to practical expertise. By engaging with these problems, you’re not merely solving puzzles; you’re mastering the craft of Bash scripting.

Feel free to ask if you have any questions or if you’d like further clarification on any of the solutions!

You Might Also Like

Basic Linux Commands for Beginners With Examples

How to Use Bash to Replace Character in String

Simple Bash Scripting Tutorial for Beginners

Difference Between Git Repository and Directory

Directory in Computer Aside from Git Bash GitHub

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Concatenated Strings in Python - Learn by Examples 20 Problems On Concatenated Strings in Python with Solutions
Next Article How to Learn C Programming - A Road map for Beginners Learn C Programming – How To Guide for Beginners

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x