GNU Bourne-Again SHell
sh-compatible language interpreter, executes commands read from an input .sh filew
Global variables can be used by all Bash scripts on your system
Local Variables can only be used within the script (or shell) in whech they’re defined
prints environment variables, which are the values kept in the machine that are in use by programs running in terminals/subshells.
ALSO prints previously specified environment variables in the shell.
aka attributes with a title and a value.
array: stores multiple data.
with index and the value of each array element accessed by its corresponding index value.
N=10
for i in $(seq 1 $N); do
echo “$i-th run”
done
i=0
while [ $i -lt 100 ]; do
i=expr $i + 1
echo $i
done
i=0
until [ $i -ge 100 ]; do
i=expr $i + 1
echo $i
done
break 1
terminates current loop, passes program control to the command that follows the terminated loop.
continue 1
skips the remaining commands in the current loop iteration, passes program control to the next loop iteration.
https://linuxhint.com/bash_select_command/
used in scripts for menu creation
#!/bin/bash
echo “Which Operating System do you like?”
Operating system names are used here as a data source
select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP
do
case $os in
# Two case values are declared here for matching
“Ubuntu”|”LinuxMint”)
echo “I also use $os.”
;;
Three case values are declared here for matching
“Windows8” | “Windows7” | “WindowsXP”)
echo “Why don’t you try Linux?”
;;
Matching with invalid data
*)
echo “Invalid entry.”
break
;;
esac
done
PS1: primary prompt string
PS2: secondary prompt string
PS3: this parameter’s value is used as prompt for the select command
PS4:
unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks
https://unix.stackexchange.com/questions/382391/what-does-unset-do
!/bin/bash
# Basic if statement if [ 1000 > 100 ] then echo Hey that\'s a large number. pwd fi date
!/bin/bash
simplifies complex conditions with multiple different choices. easier to read and maintain than nested if.
echo “Which color do you like best?”
echo “1 - Blue”
echo “2 - Red”
echo “3 - Yellow”
echo “4 - Green”
echo “5 - Orange”
read color;
case $color in
1) echo “Blue is a primary color.”;;
2) echo “Red is a primary color.”;;
3) echo “Yellow is a primary color.”;;
4) echo “Green is a secondary color.”;;
5) echo “Orange is a secondary color.”;;
*) echo “This color is not available. Please choose a different one.”;;
esac
!/bin/bash
BashFunction () {
echo Hello, my linux friends
}
BashFunction
standard input stream file. what’s input there is what’s input to the console,
what’s input to stdout is console output
“shabang”.
inscructs the program loader to use the /bin/sh program instead of any other, passing the oath of the script as the first argument.
global variables are declared outside of the function block. local variables are declared inside the function block
other source claims: all variables in bash are global by default,
set a variable as local x=$1 or sush to be a local variable.
: ‘
this will not get executed
‘
!/bin/bash
BashFunction () {
declare -A assArray1
assArray1[fruit]=Mango
assArray1[bird]=Cockatail
assArray1[flower]=Rose
assArray1[animal]=Tiger
declare -A assArray2=( [HDD]=Samsung [Monitor]=Dell [Keyboard]=A4Tech )
echo ${assArray1[bird]}
echo ${assArray2[HDD]} }BashFunction
echo $(elements[-1]}
supposedly prints the last element in the array
arr=( “apple” “banana” “cherry” )
for item in “${arr[@]}”
do
echo $item
done
echo ${#my_array[@]}
echo ${#my_array[*]}
!/bin/bash
provides the length of the element,
elements need to be seperated by spaces. the second ‘array’ simply consists of a single element ‘3,4,5,6’
testSeven () {
my_array=(3 4 5 6)
echo ${#my_array[@]}
echo ${#my_array[]}
echo stops here
my_array=(3,4,5,6)
echo ${#my_array[@]}
echo ${#my_array[]}
}
testSeven
4
4
1
1
https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
-d : true if file exists and is a directory
-w : true if file exists and is writable
-x : true if file exists and is executable
-v variable
-z${variable}