Book Image

Learn Linux Shell Scripting – Fundamentals of Bash 4.4

By : Sebastiaan Tammer
Book Image

Learn Linux Shell Scripting – Fundamentals of Bash 4.4

By: Sebastiaan Tammer

Overview of this book

Shell scripts allow us to program commands in chains and have the system execute them as a scripted event, just like batch files. This book will start with an overview of Linux and Bash shell scripting, and then quickly deep dive into helping you set up your local environment, before introducing you to tools that are used to write shell scripts. The next set of chapters will focus on helping you understand Linux under the hood and what Bash provides the user. Soon, you will have embarked on your journey along the command line. You will now begin writing actual scripts instead of commands, and will be introduced to practical applications for scripts. The final set of chapters will deep dive into the more advanced topics in shell scripting. These advanced topics will take you from simple scripts to reusable, valuable programs that exist in the real world. The final chapter will leave you with some handy tips and tricks and, as regards the most frequently used commands, a cheat sheet containing the most interesting flags and options will also be provided. After completing this book, you should feel confident about starting your own shell scripting projects, no matter how simple or complex the task previously seemed. We aim to teach you how to script and what to consider, to complement the clear-cut patterns that you can use in your daily scripting challenges.
Table of Contents (24 chapters)
Title Page
About Packt
Contributors
Preface
Free Chapter
1
Introduction
Index

Chapter 8


  1. What is a variable? A variable is a basic building block for programming languages, which is used to store run-time values that can be referenced multiple times in the application.
  2. Why do we need variables? Variables are great for storing information you need multiple times. In this case, if you need to change the information, it's a single operation (in the case of a constant). In the case of a real variable, it allows us to reference run-time information in the program.

Lastly, proper variable naming allow us to grant extra context to our script, increasing readability.

  1. What is a constant? A constant is a special type of variable, since its value is determined is fixed and used throughout the script. Normal variables are often mutated multiple times during execution.
  2. Why are naming conventions especially important for variables? Bash allows us to name our variables almost anything. Because this can become confusing (which is never a good thing!) it is important to pick one naming convention and stick to it: this increases consistency and coherence for our scripts.
  3. What are positional arguments? When you call a Bash script, any other text passed after the bash scriptname.sh command can be accessed in the script, as this text is considered the arguments to the script. Each word not enclosed in quotes is handled as a single argument: a multi-word argument should be enclosed in quotes!
  4. What is the difference between a parameter and an argument? Arguments are used to fill the parameters of a script. Parameters are the static variable names which are used in the script logic, whereas the arguments are the run-time values used as the parameters.
  5. How can we make a script interactive? By using the read command. We can store the values that the users gives in a variable of our choice, otherwise we can use the default $REPLY variable.
  6. How can we create a script that we can use both non-interactive and interactively? By combining (optional) positional arguments with the read command. To verify that we have all the information we need before starting the logic of the script, we use the if-then construct coupled with the test command to see if all our variables are populated.