- 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.
- 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.
- 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.
- 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.
- 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! - 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.
- 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. - 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 theif-then
construct coupled with thetest
command to see if all our variables are populated.