Skip to content
Technology News Information & Jobs

Technology News Information & Jobs

Ubuntu Tutorials, Ubuntu How To , Linux Tips, Tech Jobs

  • Work with US
  • Search Jobs
  • Post a Job
  • Contact Us
  • Ubuntu
    • Ubuntu 18.04
  • Ubuntu 20.04
  • Ubuntu 22.04
  • Themes
  • Linux
  • Laptops
  • Tutorials
  • Job Dashboard
  • Toggle search form
11 Bash Script Examples

11 Bash Script Examples

Posted on June 21, 2023 By admin No Comments on 11 Bash Script Examples

Bash Script Examples

Let’s have a look into some of the amazing bash scrip examples that  can be handy for you. Bash scripts are normally used for executing a shell command or running multiple commands together and also for the automation of the work. Let’s have a look into the some of the common bash script examples.

Bash Script Examples

1. Hello World Bash Script

Open any editor to create a bash file with the filename  ‘Firstexample.sh’.

nano Firstexample.sh

Add the following bash script to the file and save the file.

#!/bin/bash
echo "Hello World"

 

2. Echo Bash Script

Create a new bash file with a name, ‘echo_file.sh’ and add the following script.

#!/bin/bash
echo"Printing text along with newline"
echo -n "Printing text along without newline"
echo -e "\nRemoving \t backslash \t characters\n"

3. Using While Loop

Create a bash file with the name, ‘whileloopexample.sh’, to know the use of while loop.

#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done

4. Using For Loop:

Create a file named ‘forloop_example.sh’ and add the following script using for loop. Here, for loop will iterate for 20 times.

#!/bin/bash
for (( counter=20; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n"

5. Get User Input:

Create a file named ‘userinput_file.sh’ and add the following script to take input from the user.

#!/bin/bash
echo "Enter Your Name"
read name
echo "Welcome $name to Linux Tutorial Blog By It'subuntu"

 

READ More Relevant Stuff:  HPLIP 3.22.4 Released | HP printer and scanner drivers For Linux

6. Using if statement:

Create a file named ‘simpleif_file.sh’.

#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is one digit number"
else
echo "It is two digit number"
fi

7. Using if statement with AND logic:

Create a file named ‘if_with_AND_file.sh’ . In the following code, the ouput will be “valid users” if both values match, otherwise the output will be “invalid user”.

#!/bin/bash

echo "Enter username"
read username
echo "Enter password"
read password

if [[ ( $username == "admin" && $password == "password" ) ]]; then
echo "valid user"
else
echo "invalid user"
fi

 

8. Using if statement with OR logic:

Create a file named ‘if_with_OR.sh’ with the following code.

#!/bin/bash

echo "Enter any number"
read n

if [[ ( $n -eq 28 || $n -eq 99 ) ]]
then
echo "You have won the game"
else
echo "You have lost the game"
fi

9. Using Case Statement:

Create a new file named, ‘case_example.sh’.

#!/bin/bash

echo "Enter your lucky number"
read n
case $n in
101)
echo echo "You got 1st prize" ;;
510)
echo "You got 2nd prize" ;;
999)
echo "You got 3rd prize" ;;
*)
echo "Sorry, better try next time" ;;

 

10. Get Arguments from Command Line:

Create a file named “argcommand_line.sh” and add the following script.

#!/bin/bash
echo "Total arguments : $#"
echo "1st Argument = $1"
echo "2nd argument = $2"

 

11. Combine String variables:

Create a file named “string_combine.sh”.

#!/bin/bash

string1="Open"
string2="Source"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good than closed source"
echo $string3

 

12. Add Two Numbers:

Create a file named ‘add_numbers.sh’ with the following code. Two integer values will be taken from the user and printed the result of addition.

#!/bin/bash
echo "Enter the first number"
read x
echo "Enter the second number"
read y
(( sum=x+y ))
echo "The result of addition=$sum"
Linux

Post navigation

Previous Post: Linux Mint 21.2 Beta Is Now Available For Download
Next Post: How To Disable Automatic Updates In Ubuntu

Related Posts

200+ Free Linux Ebooks Collection For Lifetime 200+ Free Linux Ebooks Collection For Lifetime Linux
System 76 Pangolin Laptop Full Specs [2023] System 76 Pangolin Laptop Full Specs [2023] Laptops
ubuntu Useful Ubuntu Keyboard Shortcuts For Beginners Linux
Troubleshooting Linux SASL Issues Troubleshooting Linux SASL Issues Linux
Linux Commands With Examples A To Z Linux Commands With Examples Linux
GNOME 42 Desktop Environment Released With Exciting Features GNOME 42 Desktop Environment Released With Exciting Features Linux

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • New Job Search October 10, 2025
  • How To Enable Hibernate in Ubuntu 22.04 LTS May 20, 2024
  • Popular Ubuntu Games: Top 5 Games To Play March 17, 2024
  • WordPress | Your site doesn’t include support for the “generateblocks/container” block GenerateBlocks Plugin February 11, 2024
  • Ubuntu Autoremove Command Debian Autoremove Command February 10, 2024
  • How To Mount Windows 11 Shares on Debian Linux December 7, 2023
  • Anonymous Browsing 101: A Deep Dive Into Residential Proxies November 20, 2023
  • How To Correctly Install Uninstall Linux Ubuntu Nvidia Drivers Fast October 31, 2023
  • Ethical Web Design: The Dos and Don’ts October 28, 2023
  • Understanding the Role of Security Operations Center October 25, 2023
  • The Positive Effects of AI Tools and Online Services on Students Successful Essay-Writing Skills October 3, 2023

Categories

  • Android
  • Apple
  • Blogs
  • Debian
  • Fix
  • Gadgets
  • Game
  • Google
  • How To Ubuntu
  • Internet
  • Laptops
  • Linux
  • Linux Commands
  • Linux Game
  • Linux Mint
  • News
  • Operating Systems
  • Security
  • Technology
  • Themes
  • Tools
  • Tutorials
  • Ubuntu
  • ubuntu 18.04
  • Ubuntu 20.04
  • Ubuntu 22.04
  • Windows 11
  • Wordpress
itsubuntu.com is an Equal Opportunity Employer. For Advertisement, Jobs, Guest posts, and any other thoughts, email us at info@itsubuntu.com
  • Principal Architect, Accelerated Computing

    • Santa Clara, CA
    • Nvidia
    • Full Time
  • Senior AWS Architect

    • Remote
    • Shrive Technologies LLC
    • Full Time
  • Architectural Designer

    • Greenwood Village, CO
    • The Dimension Group
    • Full Time
  • Senior MDC Architect

    • Austin, TX
    • AMD
    • Full Time
  • Director of Architecture

    • Chicago, IL
    • Insight Global
    • Full Time
  • Senior Architect, NVLink

    • Westford, MA
    • Nvidia
    • Full Time
  • Architect

    • United States
    • Scout Exchange
    • Full Time
  • Computer Architecture & Systems Fundamentals Career Training Program

    • Atlanta, GA
    • Year Up United
    • Internship

Copyright © 2026 Technology News Information & Jobs.

Powered by PressBook Grid Blogs theme