BASH stands for Bourne Again Shell. It is based on bourne shell and is mostly compatible with it features.
Shells act as an intermediary between users and their operating systems, interpreting commands. Shells allow batch command execution and interactive command input, while they are not necessary for program execution. Consider a shell as a way to interact with your system, enabling you to carry out commands, launch programs, and automate difficult processes. The well-known shell BASH is merely a tool for running scripts and commands; it has nothing to do with managing files or configuring an operating system.
In its interactive mode, BASH is frequently linked to command lines and prompts. But when scripts are executed, it can also function in a non-interactive manner. Files with lists of commands that can be used to automate tasks are called scripts. These commands are often run in a consecutive manner. Learn the fundamentals of interactive shells first; you can then combine them in scripts.
A script is basically a sequence of commands in a file. Bash reads the file and processes the commands in order. It moves on to the next command only when the current one has ended.
At first #!/bin/bash is added this header is called an interpreter directive (hashbang, shebang). It specifies that /bin/bash is to be used as the interpreter when the file is used as the executable in a command.
Declaring some values to variable name:
name="Kanhaiya"
To call out the name ‘Kanhaiya’ we need to use the function echo:
echo $name #output->Kanhaiya echo "hi my name is $name" #output->hi my name is Kanhaiya echo 'hi my name is $name' #output->hi my name is $name
Editing the name:
echo “${name/K/k}” #output->kanhaiya echo “${name:0:2}” #output->ka echo “${name,}” #output->ram (1st letter lowercase) echo “${name,,}” #output->ram (all lowercase) echo “${name^}” #output->Kanhaiya (1st letter uppercase) echo “${name^^}” #output->KANHAIYA (all uppercase)
File conditions
In Bash, file conditions are checks or tests that you can perform on files or directories. Some common file conditions include
- [[-e file]] #exists
- [[-r file]] #readable
- [[-h file]] #symlink
- [[-d file]] #directory
- [[-w file]] #writable
- [[-s file]] #size is >0 bytes
- [[-f file]] #file
- [[-x file]] #executable
Chaining multiple commands or tests using && or ||
function comand1 || function comand2
if [condition1] && [condition2]; then #execute if true elif [condition3] || [condition4]; then #execute if true else #execute if all above false fi
Arrays
Arrays are used to store multiple pieces of data in one variable, which can then be extracted by using an index. Most commonly notated as var[index_position]. Arrays use indexing meaning that each item in an array stands for a number. In the array [‘car’, ‘train’, ‘bike’, ‘bus’] each item has a corresponding index. All indexes start at position 0.
Fruits=('Apple' 'Banana' 'Orange') Fruits[0]="Apple" Fruits[1]="Banana" Fruits[2]="Orange" echo "${Fruits[0]}" # Element #0 echo "${Fruits[-1]}" # Last element echo "${Fruits[@]}" # All elements, space-separated echo "${#Fruits[@]}" # Number of elements echo "${#Fruits}" # String length of the 1st element echo "${#Fruits[3]}" # String length of the Nth element echo "${Fruits[@]:3:2}" # Range (from position 3, length 2) echo "${!Fruits[@]}" # Keys of all elements, space-separated Fruits=("${Fruits[@]}" "Watermelon") # Push Fruits+=('Watermelon') # Also Push Fruits=( "${Fruits[@]/Ap*/}" ) # Remove by regex match unset Fruits[2] # Remove one item Fruits=("${Fruits[@]}") # Duplicate Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate lines=(`cat "logfile"`) # Read from file
Be First to Comment