0
0
Bash Scriptingscripting~5 mins

String variables in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a string variable in bash scripting?
A string variable in bash scripting is a named storage that holds a sequence of characters, like words or sentences, which you can use and change in your script.
Click to reveal answer
beginner
How do you assign a string value to a variable in bash?
You assign a string to a variable by writing the variable name, an equal sign, and the string without spaces around the equal sign. For example: name="Alice".
Click to reveal answer
beginner
How do you access the value of a string variable in bash?
You access the value by putting a dollar sign before the variable name, like $name. This tells the script to use the stored string.
Click to reveal answer
beginner
What happens if you put spaces around the equal sign when assigning a string variable in bash?
Bash will give an error because spaces are not allowed around the equal sign when assigning variables. It expects var=value without spaces.
Click to reveal answer
beginner
How can you include spaces inside a string variable in bash?
You put the string inside quotes, like greeting="Hello world". Quotes keep the spaces as part of the string.
Click to reveal answer
How do you assign the string 'hello' to a variable named msg in bash?
Amsg="hello"
Bmsg = "hello"
Cmsg=hello
Dmsg:hello
What does $name mean in a bash script?
AIt creates a new variable called name
BIt deletes the variable name
CIt accesses the value stored in the variable name
DIt prints the word '$name'
Which of these is a valid string variable assignment in bash?
Agreeting = Hello
Bgreeting:Hello
Cgreeting=Hello World
Dgreeting="Hello World"
What will happen if you run: name =John in bash?
AVariable name is set to 'John'
BSyntax error due to spaces around '='
CVariable name is set to '=John'
DVariable name is unset
How do you include spaces inside a string variable in bash?
AUse quotes around the string
BUse backslash before spaces
CUse parentheses around the string
DSpaces are not allowed in string variables
Explain how to create and use a string variable in bash scripting.
Think about how you store and then use a word or sentence in your script.
You got /3 concepts.
    What common mistake should you avoid when assigning string variables in bash?
    Remember how bash reads variable assignments carefully.
    You got /3 concepts.