#Bash #Linux #Networkchuck
Learn [[NC Linux|Linux Basics]] to understand bash scripting.
It stands for Bourne Again Shell.
bash example.sh
If you want to run it without using using bash
, give the file suitable [[NC Linux#^1927f0|permissions]].
sudo chmod +x example.sh
./example.sh
.
refers to the current directory.
#!
is known as shebang.
which $SHELL
- Displays the type of shell we are in now.
#!/bin/bash
# This is a comment
echo "This is a bash script. Write your linux commands in this file."
echo "Sleeping for 5 sec"
sleep 5
echo "End of program"
#!/bin/bash
name="kathir"
echo "Hello $name"
sleep 1
echo "How are you $name?"
#!/bin/bash
echo "What's your name:"
read name
echo "Hello $name"
sleep 1
echo "How are you $name?"
#!/bin/bash
name=$1
echo "Hello $name"
sleep 1
echo "How are you $name?"
We obtain inputs form user through Positional arguments.
./example.sh kathir
#!/bin/bash
user=$(whoami)
date=$(date)
echo "You are logged in as $user"
echo "Today's date is $date"
They are built-in variables.
$RANDOM
- Returns a random integer between 0 and 32767
$SHELL
- Returns the current shell being used
$USER
- Name of the logged in user
$PWD
- Returns the present working directory
$OLDPWD
- Returns the previous working directory
$_
- Returns the previously executed command
You can view these variables by using echo
and can even use in your bash script.
export name="kathir"
By using the export
command, the variable you created can be accessed by any script.
These variables are forgot once the system is rebooted. To make these variables permanent, add the previous command to the .bashrc
file in your home directory and reboot to see the changes.
echo $((2+3)) $((2-3)) $((2*3)) $((10/3)) $((10%3))
#!/bin/bash
sysnum=$(($RANDOM % 2))
echo "Pick 0 or 1:"
read usrnum
echo $sysnum
if [[ $sysnum == $usrnum && $USER != "root" ]]; then
echo "Same"
else
echo "Different"
exit 1
fi
echo "Enter any integer:"
read num
if [[ $num > 0 ]]; then
echo "Positive"
elif [[ $num < 0 ]]; then
echo "Negative"
else
echo "Zero"
fi
Tab is not a necessary syntax but is included for better readability.
||
- or
&&
- and
There are also break
and continue
statements.
Runs while the condition is true
#!/bin/bash
x=1
# -le is 'lesser than or equal to'
while [[ $x -le 10 ]]
do
echo $x
(( x++ ))
done
while true
do
echo "Infinite Looping..."
((x--))
if [[ $x == 0 ]]; then
break
fi
done
Runs until the condition is true.
#!/bin/bash
until [[ $num == 0 ]]
do
echo "Do not enter 0:"
read num
done
#!/bin/bash
for i in {1..21..3};
do
echo $i
done
for name in a b c d e f g h i j k;
do
echo $name
done
for city in $(cat cities.txt);
do
echo $city
done
#!/bin/bash
function hello() {
echo "Hello World Function!"
}
hello
Passing arguments to a function work in the same way as passing arguments to a script:
#!/bin/bash
function hello() {
echo "Hello $1!"
}
hello kathir