Chapter 5: Bash Scripting Flashcards
What do bash scripts begin with?
!/bin/bash
How do we execute a script called “Hello-World.sh”?
./Hello-World.sh
How do we make the script executable?
chmod -x script.sh
How do we set a variable?
first_name=good
How do we call the variable?
$first_name
What is wrong with the follow command? good=Hello World
There is a space between Hello and World which causes the command to declare a variable and process World as a separate command. This can be remedied with single or double quotes.
What is the difference between single and double quotes in bash scripting?
Single quotes processes every character literally while double quotes processes every character except for $ ` and . Without quotes, spaces function as command delimiters.
What is command substitution?
Command substitution is setting the value of a variable to be the output of a certain program or command.
How do we use command substitution? Use an example.
An example would be user=$(whoami)
The command must be surrounded by brackets and preceded outside the brackets with a dollar sign.
How do we write the first and second arguments within a bash script?
$1 and $2
How many arguments can be put into a bash script?
- Using $1 … $8 $9
Which command allows us to capture user input? Use an example.
The read command. The word proceeding it is turned into a variable later on in the script and can be called upon.
Example:
read answer
echo “Your answer was $answer”
Within the read command, how do we specify a prompt?
-p
Within the read command, how do we specify a prompt and make the input silent as the user is typing it in?
-sp
Explain the if statement. Give an example.
The if statement checks to see if a condition is true. The syntax needs to be precise. For example:
if [ ]
then
fi