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:  bmon Network Bandwidth Monitoring And Debugging Tool

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

Mozilla VPN Is Now Available To Linux Users Mozilla VPN Is Now Available To Linux Users Linux
Microsoft Wants To Bring Defender For Linux Users Linux
Best Linux CCTV Camera Software Best Linux CCTV Camera Software Linux
AV Linux MX-21 Multimedia Production Distro Released AV Linux MX-21 Multimedia Production Distro Released Linux
Best BitTorrent Clients For Linux In 2019 Best BitTorrent Clients For Linux In 2019 Linux
How To Remove Files And Directories In Linux [Examples] How To Remove Files And Directories In Linux [Examples] Linux

Leave a Reply Cancel reply

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

Recent Posts

  • 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
  • Linux Wget Examples September 27, 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
  • Search Jobs
  • Post a Job
  • Job Dashboard
  • Contact Us
  • About US
  • Cookie Policy
  • Terms and Conditions
  • Privacy Policy
  • Sitemap
  • Write For US

Tags

android apps centos ChatGPT command Commands Debian deepin Desktop docker Fedora fix game games Gaming gnome google kernel linux linux commands linux kernel Linux laptops Linux Mint Linux themes minecraft NVIDIA Python Raspberry Pi sysadmin themes ubuntu ubuntu 18.04 Ubuntu 18.04 LTS Ubuntu 20.04 ubuntu 20.04 lts Ubuntu 22.04 LTS Ubuntu games Ubuntu kernel ubuntu themes ubuntu tutorial VirtualBox Windows Windows 11 Wine WSL
itsubuntu.com is an Equal Opportunity Employer. For Advertisement, Jobs, Guest posts, and any other thoughts, email us at [email protected]
  • Prepared Foods Order Writer (Deli / Culinary – Buyer / Inventory Replenishment) – Full Time

    • Thayne, WY
    • Whole Foods Market
    • Full Time
  • Sr. Risk and Controls Compliance Policy Writer (on-site Phoenix, Chicago, San Fran)

    • San Francisco, CA
    • INSPYR Solutions
    • Full Time
  • Artificial Intelligence & Gen AI Engineer – Tampa, Hybrid

    • Tampa, FL
    • Citigroup Inc
    • Full Time
  • Sr. Technical Documentation Specialist – REMOTE

    • Atlanta, GA
    • HHS Technology Group, Inc.
    • Full Time
  • TECHNOLOGY VICE PRESIDENT, ARTIFICIAL INTELLIGENCE & MACHINE LEARNING AND DATA

    • Falls Church, VA
    • NLP PEOPLE
    • Full Time
  • Architectural Drafter

    • Sarasota, FL
    • Start To Finish Drafting
    • Full Time
  • Senior or Principal Data Scientist – Technical AI Ethicist

    • Seattle, WA (Remote)
    • salesforce.com, inc.
    • Full Time
  • Artificial Intelligence (AI) Data Scientist

    • Fairfax, VA
    • General Dynamics Information Technology
    • Full Time

Copyright © 2025 Technology News Information & Jobs.

Powered by PressBook Grid Blogs theme