0% found this document useful (0 votes)
85 views

Bash SHELL Script in Linux

The document provides examples of common bash script commands and functions, including: 1) Printing "Hello World", creating and running bash scripts, and using echo commands. 2) Adding comments, using while, for, and if/else loops with conditional statements. 3) Taking user input, defining functions, and piping commands. 4) Examples demonstrate basic bash script syntax and commands for printing output, running scripts, looping, conditional checks, and more.

Uploaded by

Hesti Widya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Bash SHELL Script in Linux

The document provides examples of common bash script commands and functions, including: 1) Printing "Hello World", creating and running bash scripts, and using echo commands. 2) Adding comments, using while, for, and if/else loops with conditional statements. 3) Taking user input, defining functions, and piping commands. 4) Examples demonstrate basic bash script syntax and commands for printing output, running scripts, looping, conditional checks, and more.

Uploaded by

Hesti Widya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Example command of Bash script in Linux

 Open Linux ‘Terminal’ and write the bash command/script in it


 Print Hello world
$ echo "Hello World"

 Create file in bash


Open any editor to create a bash file. Here, nano editor is used to create the file and
filename is set as ‘First.sh’
$ nano First.sh
Add the following bash script to the file and save the file

#!/bin/bash
echo "Hello World"

 Run Command
You can run bash file by two ways. One way is by using bash command and another
is by setting execute permission to bash file and run the file. Both ways are shown
here:

$ bash First.sh

or,
$ chmod 755 file.sh
$ chmod a+x First.sh
$ ./First.sh
 Echo command
You can use echo command with various options. Some useful options are
mentioned in the following example. When you use ‘echo’ command without any
option then a newline is added by default. ‘-n’ option is used to print any text without
new line and ‘-e’ option is used to remove backslash characters from the output.
Create a new bash file with a name, ‘echo_example.sh’ and add the following script.
#!/bin/bash
echo "Printing text with newline"
echo -n "Printing text without newline"
echo -e "\nRemoving \t backslash \t characters\n"
Run the file with bash command.

 Comment in bash
‘#’ symbol is used to add single line comment in bash script. Create a new file named
‘comment_example.sh’ and add the following script with single line comment.
#!/bin/bash

# Add two numeric value


((sum=25+35))

When running the file, the line with hash will not be printed.

For Multiple comment uses colon and single quote:


:’
This is example of comment

 While Loop
Create a bash file with the name, ‘while_example.sh’, to know the use
of while loop. In the example, while loop will iterate for 5 times. The value
of count variable will increment by 1 in each step. When the value of count variable
will 5 then the while loop will terminate.
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
Result:
$ bash while_example.sh

 For – Loop
The basic for loop declaration is shown in the following example. Create a file named
‘for_example.sh’ and add the following script using for loop. Here, for loop will
iterate for 10 times and print all values of the variable, counter in single line.
Example script:
#!/bin/bash
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n"

Result:
$ bash for_example.sh

 Get user input


‘read’ command is used to take input from user in bash. Create a file named
‘user_input.sh’ and add the following script for taking input from the user. Here, one
string value will be taken from the user and display the value by combining other
string value.
#!/bin/bash
echo "Enter Your Name"
read name
echo "Welcome $name to LinuxHint"

Result:
$ bash user_input.sh

 IF statement
You can use if condition with single or multiple conditions. Starting and ending block
of this statement is define by ‘if’ and ‘fi’. Create a file named ‘simple_if.sh’ with the
following script to know the use if statement in bash. Here, 10 is assigned to the
variable, n. if the value of $n is less than 10 then the output will be “It is a one digit
number”, otherwise the output will be “It is a two digit number”. For comparison, ‘-
lt’ is used here. For comparison, you can also use ‘-eq’ for equality, ‘-ne’ for not
equality, ‘-gt’ for greater than, and ‘-lt’ for less than in bash script.
example script:
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi

 AND condition in IF statement


Different types of logical conditions can be used in if statement with two or more
conditions. How you can define multiple conditions in if statement using AND logic is
shown in the following example. ‘&&’ is used to apply AND logic of if statement.
Create a file named ‘if_with_AND.sh’ to check the following code. Here, the value
of username and password variables will be taken from the user and compared with
‘admin’ and ‘secret’. If both values matched then the output will be “valid user”,
otherwise the output will be “invalid user”.

#!/bin/bash

echo "Enter username"


read username
echo "Enter password"
read password

if [[ ( $username == "admin" && $password == "secret" ) ]]; then


echo "valid user"
else
echo "invalid user"
fi

 OR condition in IF statement
‘||’ is used to define OR logic in if condition. Create a file named ‘if_with_OR.sh’ with
the following code to check the use of OR logic of if statement. Here, the value
of n will be taken from the user. If the value is equal to 15 or 45 then the output will
be “You won the game”, otherwise the output will be “You lost the game”.
#!/bin/bash

echo "Enter any number"


read n

if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi
 ELSE IF statement
The use of else if condition is little different in bash than other programming
language. ‘elif’ is used to define else if condition in bash. Create a file named,
‘elseif_example.sh’ and add the following script to check how else if is defined in
bash script.

!/bin/bash

echo "Enter your lucky number"


read n

if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize"

else
echo "Sorry, try for the next time"
fi

 Function in shell script


A function can be created in bash, using keyword ‘function’ then function_name.
The function will be running when its name is declared.
Example code

!/bin/bash

echo "This is function"

function hello() {
echo “Hello world”
}

#call the function


hello

when run, it will print the text/data declared inside the function

 Piping in shell script


!/bin/bash
function GetFiles() {
FILES=’ls -1 | sort | head -10’
}
GetFiles
#for other command:
https://linuxhint.com/30_bash_script_examples/#t1

Note:
Using parameter -> allowing user to add parameter to variable
Example parameter:
Name=$1
Name=$@ (multiple parameter values)

$(date) -> shows current date & time


-z TO CHECK EMPTY string/variable

Environment variable -> help to know the system environment, establish before our
script run
List of environment variables:
$PATH ->file path/directory
$TERM -> Terminal
$EDITOR -> Editor
$HOME -> user’s home directory
$HOSTNAME -> name of the computer
$SHELL -> shell which is being use
$USER

sort -> order file ascending


sort -r -> order files in reverse/desc
head n -> return the top file or data

write text in shell script


using ‘>’ sign: to write text from one file to another, but remove the data inside the
target file
using ‘>>’ sign: to write text from one file to another without deleting previous data in
file

#checksum is to check integrity of file


In terminal, run command:
cksum file_name
it shows: checksum_value file_size_inByte file_name

You might also like