Interview › Scripting (Bash, Groovy)
How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
Answer
A Bash variable is assigned as name=value with no spaces around the equals sign. Spaces are not allowed because the shell parses spaces as command separators; var = value is interpreted as running a command named var with arguments = and value.
Technical explanation
Use uppercase names for exported environment variables and lowercase or mixed case for local script variables to avoid conflicts with system variables.
Always quote variable expansions unless you intentionally want word splitting or glob expansion.
Use readonly for constants and local inside functions to limit scope.
Hands-on example
#!/usr/bin/env bash
app_name="orders-api"
readonly region="ap-south-1"
echo "Deploying ${app_name} to ${region}"
# Wrong: app_name = orders-api # tries to execute app_name
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]
- Why should you quote variables, and what bug does unquoting cause? [Basic]