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"
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"