0
0
Bash Scriptingscripting~5 mins

Variable assignment (no spaces around =) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
Why should there be no spaces around the = sign in bash variable assignment?
In bash, spaces around = are not allowed because bash treats spaces as separators. Spaces would cause bash to misinterpret the command, leading to errors.
Click to reveal answer
beginner
What is the correct way to assign the value hello to a variable named greeting in bash?
The correct way is greeting=hello. No spaces should be before or after the = sign.
Click to reveal answer
beginner
What happens if you write greeting = hello in bash?
Bash will try to run a command named greeting with arguments = and hello, causing an error because greeting is not a command.
Click to reveal answer
intermediate
How do you assign a value with spaces to a variable in bash?
You put the value in quotes without spaces around =. For example: message="hello world".
Click to reveal answer
beginner
Is this valid in bash? name= John
No, it is invalid because there is a space after =. The correct form is name=John.
Click to reveal answer
Which of these is the correct way to assign the value 10 to a variable x in bash?
Ax= 10
Bx = 10
Cx=10
Dx =10
What will happen if you run var =value in bash?
AAssign '=value' to 'var'
BAssign 'value' to 'var'
CSyntax error
DRun command 'var' with argument '=value'
How do you assign the string 'hello world' to a variable message in bash?
Amessage="hello world"
Bmessage = "hello world"
Cmessage= 'hello world'
Dmessage=hello world
Which is NOT a valid variable assignment in bash?
ABoth B and D
Bcount =5
Ccount=5
Dcount= 5
Why does bash require no spaces around '=' in variable assignment?
ABecause '=' is a special operator that must be attached to the variable name
BBecause spaces separate commands and arguments
CBecause spaces cause syntax errors in all programming languages
DBecause '=' is a reserved word
Explain why spaces are not allowed around the equals sign in bash variable assignment.
Think about how bash reads commands and arguments.
You got /3 concepts.
    Show how to correctly assign a string with spaces to a variable in bash.
    Remember how to handle strings with spaces.
    You got /3 concepts.