Chapter 2: Bash Basics and Syntax
Understanding the basics of Bash syntax, commands, and structure, including how to write and execute scripts, set permissions, and work with basic Bash commands.
Dive deeper into the syntax of Bash scripting. This chapter covers essential commands, variables, control structures, and script execution to enhance your Bash scripting skills.
Essential Commands in Bash
Bash offers a set of core commands that allow you to interact with the system and manipulate files. Here are some fundamental commands:
- echo: Outputs text to the terminal. For example,
echo "Hello, World!"
- pwd: Displays the current directory path.
- cd: Changes the current directory.
- ls: Lists files and directories within a specified directory.
- cp: Copies files or directories from one location to another.
- mv: Moves or renames files and directories.
Understanding Bash Syntax
In Bash, scripts are interpreted line by line, and syntax accuracy is crucial. Below are key syntax components:
- Comments: Lines beginning with
#
are ignored by Bash and are used for documentation. For example,# This is a comment
. - Commands: Bash scripts consist of commands that are executed in sequence, such as file manipulation and data processing commands.
- Exit Codes: Every command returns an exit code indicating success (0) or failure (non-zero).
Working with Variables
Variables are used to store data values, which can be accessed and modified throughout the script. Here’s how to declare and use variables:
# Declaring a variable
greeting="Hello, Bash!"
# Accessing a variable
echo $greeting
In the above example, the variable greeting
stores the string "Hello, Bash!"
and is accessed using the $
symbol.
Control Structures
Control structures, such as conditionals and loops, allow scripts to make decisions and repeat actions:
Conditionals
Conditionals use if
statements to execute code based on specific conditions:
# Example of an if statement
num=5
if [ $num -gt 3 ]; then
echo "Number is greater than 3"
else
echo "Number is 3 or less"
fi
Loops
Loops allow for repetition of tasks, such as iterating over files or data sets:
# Example of a for loop
for i in {1..5}; do
echo "Loop iteration $i"
done
Setting Permissions and Executing Scripts
To run a Bash script, you need to ensure it has the correct permissions. Here’s a step-by-step guide:
- Create a new file and write your script, such as
script.sh
. - Make the script executable with
chmod +x script.sh
. This command grants execute permissions. - Run the script using
./script.sh
in the terminal.
By setting permissions, you allow the script to be executed in the terminal. This step is essential for running custom scripts in your environment.
Summary and Next Steps
In this chapter, we covered Bash basics, including essential commands, variables, control structures, and script execution with permissions. These fundamentals provide a strong foundation for more advanced scripting topics, which we'll cover in the next chapter on control flow in Bash scripting.