Part 2: Basic Bash Scripting Concepts
Welcome back to our “Mastering Bash Scripting for Embedded Linux Development” series! In this second part, we’ll delve into some fundamental concepts of Bash scripting. We’ll cover variables and data types, command substitution, control structures (if statements and loops), functions, and error handling in detail, accompanied by examples to illustrate each concept.

Variables and Data Types
Variables in Bash are used to store data, such as strings, numbers, or arrays. Variable names are case-sensitive and can contain letters, numbers, and underscores. Let’s look at an example:
# Assigning a string to a variable
my_variable="Hello, world!"
# Accessing the variable
echo $my_variable
Bash supports several data types, including strings, integers, and arrays. Unlike many programming languages, Bash does not require explicit declaration of variable types.
# Integer variable
my_number=42
# Array variable
my_array=("apple" "banana" "orange")
Syntax Overview:
- When referring to a variable, use
$variable. - When setting a variable, omit the
$sign. - Variable names can be in uppercase, lowercase, or mixed case, but consistency is key.
- Variables can be placed anywhere in the script, and Bash will replace them with their values during execution.
Command Substitution
Bash scripts can accept command line arguments, allowing for dynamic behavior based on user input. These arguments are represented by special variables:
$1to$9represent the first nine command line arguments.$#indicates the total number of arguments passed.$@represents all arguments passed.$0denotes the script’s name.
Utilizing these variables, you can create scripts that respond to user inputs flexibly, enhancing their versatility and usability.
Other Special Variables
In addition to command line arguments, Bash provides a range of other special variables that offer insights into script execution and system environment. These include:
$?: Exit status of the most recently executed command.$$: Process ID of the current script.$USER: Username of the script’s executor.$HOSTNAME: Hostname of the machine running the script.- And more, each serving a specific purpose in script execution and management.
Understanding and leveraging these variables can significantly enhance script functionality and robustness.
#!/bin/bash
echo "Welcome to $1"
And run it using the parameter as shown:
$ script1.sh "Embed Threads."
#Output
Welcome to Embed Threads.
Note: There are two primary syntaxes for command substitution:
1) Backticks (` `)
variable=`command`
2) Dollar Sign and Parentheses ($())
variable=$(command)
Both syntaxes achieve the same result, but the latter ($()) is preferred due to its readability and compatibility with nested substitutions.
Control Structures: If Statements and Loops
If statements and loops are used to control the flow of execution in Bash scripts.
If Statements:
# Check if a variable is empty
if [ -z "$my_variable" ]; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi
Loops:
While Loop:
# While loop to count from 1 to 5
count=1
while [ $count -le 5 ]; do
echo $count
((count++))
done
For Loop:
# For loop to iterate over elements in an array
for fruit in "${my_array[@]}"; do
echo "I like $fruit"
done
Functions
Functions allow you to group code into reusable blocks. They can accept arguments and return values, although Bash functions are more limited compared to functions in other programming languages.
# Define a function to greet someone
greet() {
echo "Hello, $1!"
}
# Call the function
greet "John"
This will output “Hello, John!”.
Error Handling
Error handling in Bash involves checking the exit status of commands and reacting accordingly. You can use the exit command to terminate the script with a specific exit status, and the special variable $? to capture the exit status of the last command.
# Check if a command succeeded
ls non_existent_file
if [ $? -eq 0 ]; then
echo "Command executed successfully"
else
echo "Command failed"
fi
Conclusion
In this part of our series, we’ve covered some basic Bash scripting concepts, including variables and data types, command substitution, control structures (if statements and loops), functions, and error handling. These concepts form the foundation of Bash scripting and will serve you well as you continue to explore more advanced topics in future parts of the series.
Stay tuned for the next installment, where we’ll dive deeper into working with files and directories in Bash scripting. In the meantime, feel free to experiment with these concepts and expand your Bash scripting skills. Happy scripting!
Assignment 1
In this homework assignment, you will apply the fundamental concepts of Bash scripting covered in our recent blog post. You’ll be tasked with creating several scripts that demonstrate your understanding of variables, command substitution, control structures, functions, and error handling in Bash.
Instructions:
1) Variable Practice:
- Create a Bash script called
variable_practice.sh. - Declare variables of different types (string, integer, array) and print their values.
- Perform operations on variables (e.g., arithmetic operations, string manipulation).
- Comment your code to explain each step.
2) Command Substitution:
- Develop a Bash script named
command_substitution.sh. - Use command substitution to capture the output of a command.
- Demonstrate at least three different use cases of command substitution.
- Add comments to clarify the purpose of each command.
3) Control Structures:
- Write a script called
control_structures.sh. - Implement an if statement to check whether a file exists. If it does, print a message indicating its existence; otherwise, create the file and notify the user.
- Utilize a loop structure (while or for) to iterate through an array of strings and print each string.
- Ensure your script handles both scenarios (file exists and doesn’t exist) gracefully.
4) Functions:
- Develop a script named
functions.sh. - Define a function that takes two integers as arguments and returns their sum.
- Call the function with different input values and print the results.
- Include comments to explain the purpose of the function and its parameters.
5) Error Handling:
- Create a Bash script called
error_handling.sh. - Implement error handling to check if a required command or program is installed on the system.
- If the command/program is missing, display an error message; otherwise, proceed with the script execution.
- Document your code to explain the significance of error handling in Bash scripting.
paypal casino
References:
https://internskill.in/companies/play-paypal-pokies/
online casino paypal einzahlung
References:
https://fmagency.co.uk/companies/best-paypal-casinos-2025-100-real-money-paypal-sites-%ef%b8%8f/
Casino bonus with no deposit is credited to players’ accounts every Thursday.
Winspirit Casino is famous for its generous bonuses and
promotional offers for both new and regular players.
If you have trouble, contact live chat for help with winspirit casino login.
The casino processes payouts manually, meaning the withdrawal times may vary, but once approved, crypto and PayID withdrawals tend
to be the fastest. With an RTP close to 96.5%, it’s popular among
players looking for high-energy spins and explosive wins.
New members do not enter the VIP Club automatically
— the team grants invitations based on account activity, deposit history, and responsible play patterns.
These cashback bonuses typically come with low or even zero wagering requirements, making them highly appealing.
References:
https://blackcoin.co/neosurf-casinos-australia-a-comprehensive-guide/
So, some players are looking for the best sites that offer popular pokies for
Aussie players, while others look for specific payment casinos
and sites with provably fair games. Any gains from online casinos
are not liable to taxes for Australian players, as gambling is
seen as a leisure activity rather than a career. That’s a real letdown when you compare
it to top Australian online casinos like DragonSlots and AllStar, which are stacked with all sorts of bonuses.
We have also including guides to everything you need to know about online gambling from
banking to live dealer casinos, as well as all the latest
news from Australia regarding the gambling scene. Online
casinos features every game you could play in a brick-and-mortar casino, plus additional games.
Each review includes information about individual casinos, sign-up bonuses, deposit options, and our ratings.
References:
https://blackcoin.co/mr-green-erfahrungen/
Das New York New York Hotel & Casino verfügt über einen großen saisonalen Poolbereich,
der eine willkommene Abkühlung mitten in der Wüste bietet.
(Es ist unnötig zu erwähnen, dass das New
York, New York auch über keine edlen Promirestaurants verfügt.) Erlebe den ultimativen Luxus in Las Vegas mit unseren handverlesenen Luxushotels.
Wie bei den meisten Themenhotels in Las Vegas sind die Zimmer (dieses
Hotel hat etwa 2.000) nicht die größte Attraktion,
obwohl sie durchaus angemessen sind.
Dank der Lage des Hotels in der Innenstadt von New York lässt sich New York Transit Museum bequem erreichen. In 200 Meter
Entfernung zur U-Bahn-Station Nevins St gelegen, bietet das
EVEN Hotel Brooklyn New York 202 klimatisierte Zimmer mit allen notwendigen Annehmlichkeiten. Das Hotel befindet sich
150 Meter von der U-Bahn-Station Hoyt-Schermerhorn Sts
entfernt. Intrepid Sea liegt in unmittelbarer Nähe zum Hotel.
References:
https://online-spielhallen.de/vinyl-casino-deutschland-ihr-tor-zur-musikalischen-glucksspielwelt/