Bash is a command language interpreter. It is the default command interpreter on most GNU/Linux systems. The name is an acronym for the ‘Bourne-Again SHell’.
Scripting allows for automatic command execution that would otherwise take many manually executed interactive steps.
alias name="command"
echo $SHELL
OR
echo $0
>~ cat /etc/shells
# Pathnames of valid login shells.
# See shells(5) for details.
/bin/sh
/bin/bash
/usr/bin/git-shell
/bin/zsh
/usr/bin/zsh
#!/bin/bash for bash
# Always starts with a shebang
# This is a comment
echo "Hello, World!"
#!/usr/bin/python
chmod +x script_name
chmod 700 script_name
bash script_name.sh
(runs without execute permission)./script_name.sh
python3 script_name.sh
(runs without execute permission)source script_name.sh
(execute permission is not required)./
runs the script in sub shell and calling it runs in current shellos=Linux |
os = Linux is not allowed : no spaces |
""
os="Debian Bullseye"
version=11
.
in a name@
in a name_
the value of the variable can be referenced using $variable_name
echo $os
echo "I am learning $os"
""
and not ''
> echo "I am learning $os"
I amlearning Linux
NOTICE THE DIFFERENCE with ""
and ''
> echo 'I am learning $os'
I am learning $os
\$
can be used as an escape character to reference variable names instead of variable values.
> echo "The value of \$os is $os"
The value of $os is Linux
set
to list all the shell variables and pipe it to less
or grep
depending on the use case.declare
keyword with -r
flag to create a read only variable
declare -r logdir="/var/log"
ls $logdir
> logdir=abc
bash: logdir: readonly variable
Each time we launch a terminal window and a shell inside it, a collection of predefined variables are set with dynamic values which are used to customize how a system works. There are two class of such variables
env
or print env
$PATH
: path variable$USER
: Current logged in user$HOME
: Home directory of $USER
$HISTFILE
: Shell command history file$HISTSIZE
: Max length of $HISTFILE
export
it in a startup config like .bashrc
export PATH=$PATH:/path_to_dir/
export MYVAR=value
Shell Variables
set
/etc/bash.bashrc
and /etc/profile
read
: read from standard input into shell variables > read name
enter_name_here
-p
flag to echo a prompt or hint > read -p "Enter your name please: " name
enter_name_here
$name
like echo $name
-s
to hide the user input, for example a password.Context
apt install neofetch
, install
is the first argument in the script and neofetch
is the second argument.space
referred to as inter field separators.Example: In ./script.sh filename1 dir1 some_value other_value
If, Elif and Else Statements
[[ ]]
is a newer safer way to do test conditions as it works well with strings with spaces and allows for regex matching. But it also works with []
if [[ this_condition_is_true ]]
then
execute_this_code
elif [[ some_ther_condition_is_true ]]
then
execute_this_code
else
execute_this_code
fi
-f
is a file-d
is a directory INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
#!/bin/bash
read -p "Enter and number: " guess
if [[ $guess -eq 5 ]]
then
echo "Jackpot"
else
echo "Okay looser!"
fi
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
#!/bin/bash
read -p "String1: " str1
read -p "String2: " str2
########################
# Method 1
########################
if [ "$str1" = "$str2" ]
then
echo "Equal strings"
else
echo "Not Equal strings"
if
########################
########################
# Method 2
########################
if [[ "$str1" == "$str2" ]]
then
echo "Equal strings"
else
echo "Not Equal strings"
if
########################
str1="some strings with linux in it."
if [[ '$str1' == *"linux"* ]]
then
echo "The substring linux IS present."
else
echo "The substring linux IS NOT present."
fi
For multiple conditions
or
and and
operators can be used
or
is ||
and
is &&
[[ condition 1 ]] && [[ condition 2 ]]
[[ condition 1 ]] || [[ condition 2 ]]
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
Anatomy of a nested if statement
if [[ condition ]]
then
if [[ condition 2]]
then
do_this
else
do_this
else
do_this
fi
for item in LIST
do
COMMANDS
done
#!/bin/bash
for os in Ubuntu CentOS Slackware Kali "MX Linux"
do
echo "OS is $os"
done
{start_num..end_num}
{start_num..end_num..step
for num in {3..7}
do
echo "num is $num"
done
for item in ./*
do
if [[ -f "$item" ]]
then
echo "$item"
fi
done
while CONDITION_IS_TRUE
do
COMMANDS
done
i=0
while [[ $i -lt 10]]
do
echo "i: $i"
((i++))
done
#!/bin/bash
while true
do
echo "In an infinite loop, press CTRL+C to exit."
sleep 1
done
while [[ true ]]
while :
while true; do echo "infinite loop"; sleep 1; done
# Method 1
var_one=`command`
# Method 2
var_two="$(command)"
((arithmetic_operation))
switch
statement in C, C++ & Javacase
statement
PATTERN
can contain special charactersSTATEMENT
blocks end with ;;
(double semicolon)*
PATTERN is a default case case EXPRESSION in
PATTERN_1)
STATEMENTS
;;
PATTERN_2)
STATEMENTS
;;
PATTERN_3)
STATEMENTS
;;
PATTERN_4)
STATEMENTS
;;
""
""
*)
STATEMENTS
;;
esac
option_1|option_2
"A String With Spaces"
inside ""
#!/bin/bash
echo -n "Enter your favorite pet: "
read PET
case "$PET" in
dog)
echo "Your favorite pet is the dog."
;;
cat|Cat)
echo "You like cats."
;;
fish|"African Turtle")
echo "Fish or turtles are great."
;;
*)
echo "Your favorite pet is weird"
esac
signal.sh
#!/bin/bash
if [[ $# -ne 2 ]]
then
echo "Run the script with 2 arguments: Signal and PID."
exit
fi
case "$1" in
1)
echo "Sending the SIGHUP signal to $2"
kill -SIGHUP $2
;;
2)
echo "Sending the SIGINT signal to $2"
kill -SIGINT $2
;;
15)
echo "Sending the SIGTERM signal to $2"
kill -15 $2
;;
*)
echo "Signal number $1 will not be delivered"
esac
()
in functions definitions are purely decorative
$varibale_name
or $1
$2
etc. #!/bin/bash
function name_of_function () {
one thing to do
two thing to do
}
OR
name_of_function () {
do_something
}
# This is a function call
name_of_function
local
var1-"AA"
var2="BB"
func1() {
var1="XX"
local var2="YY"
echo "Inside func1: var1=$var1, var2=$var2"
}
func1
echo "Outside func1: var1=$var1, var2=$var2"
Inside func1: var1=XX, var2=YY
Outside func1: var1=XX, var2=BB
"string contains a space"
enclose it in ""
REPLY
#?
and can be change by setting a predefined variable PS3
select ITEM in LIST
do
COMMANDS
done
#!/bin/bash
PS3="Choose your country"
select COUNTRY in India Germany France USA "United Kingdom" Quit
do
case $REPLY in
1)
echo "You speak many languages"
;;
2)
echo "You speak many languages"
;;
3)
echo "You speak many languages"
;;
4)
echo "You just speak amarican english"
;;
5)
echo "You speak many languages"
6)
echo "Qutting..."
break
;;
*)
echo "Invalid Option"
done
Github Repo: bass-scripting-basics