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.
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!