Bash Scripting
What it is?
Bash is the scripting language we use to communicate with Unix-based OS and give commands to the system.
Script Execution - Examples
rio@kali[/kali]$ bash script.sh <optional arguments>
rio@kali[/kali]$ sh script.sh <optional arguments>
rio@kali[/kali]$ ./script.sh <optional arguments>Shebang
The #!/bin/bash at the beginning of a Bash script is known as a shebang or hashbang. It serves as a directive to the operating system, indicating which interpreter should be used to execute the script.
Conditional Execution
If-Else
if [condition]
then [execution]
else [what would be executed if condition would fail]
fi [closing]If-Only
If-Elif-Else
Case
Example::
Comparison Operators
-eq
equal to
-ne
not equal to
-lt
less than
-le
less than or equal to
-gt
greater than
-ge
greater than or equal to
String Operators
==
is equal to
!=
is not equal to
<
is less than in ASCII alphabetical order
>
is greater than in ASCII alphabetical order
-z
if the string is empty (null)
-n
if the string is not null
File Operators
-e
if the file exist
-f
tests if it is a file
-d
tests if it is a directory
-L
tests if it is if a symbolic link
-N
checks if the file was modified after it was last read
-O
if the current user owns the file
-G
if the file’s group id matches the current user’s
-s
tests if the file has a size greater than 0
-r
tests if the file has read permission
-w
tests if the file has write permission
-x
tests if the file has execute permission
Special Variables
$#
Number of arguments passed to the script.
$@
List of command-line arguments.
$n
n is number of argument
$$
Id of executing process
$?
Success of command. 0 is success, 1 is a failure
Regular Variables
Arrays
OR
Loops
For
Example:
While
Example:
Until
Example:
Script Termination
exit 0
Succesful execution
exit 1
General error condition
exit 2
Specific error condition
Wildcards
In Bash, a wildcard refers to a character or a set of characters that can be used to represent a group of filenames or strings. Wildcards are often used in commands to perform operations on multiple files or strings that match a specified pattern.
* (Asterisk)
echo *.txt
Matches all files ending with ".txt".
? (Question Mark)
ls file?.txt
Matches files like "file1.txt", "fileA.txt", etc.
[ ] (Square Brackets)
ls [aeiou]*.txt
Matches any file starting with a vowel and ending with ".txt".
{ } (Brace Expansion)
cp file{1,2,3}.txt dest/
Expands to "file1.txt", "file2.txt", and "file3.txt" and copies to the destination.
!(pattern) (Extended Pattern Matching)
ls !(file*.txt)
Matches all files except those starting with "file" and ending with ".txt".
?(pattern) (Zero or One Occurrence)
ls file?(1).txt
Matches "file.txt" or "file1.txt".
+(pattern) (One or More Occurrences)
ls file+(1).txt
Matches "file1.txt", "file11.txt", etc.
*(pattern) (Zero or More Occurrences)
ls file*(1).txt
Matches "file.txt", "file1.txt", "file11.txt", etc.
Tips & Tricks
You Could use
teecommand for writing output to both standard output and file. If you would usetee -a, it wouldUse
bash -x -vto verbose debugging
Last updated