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

Preparing for an interview?

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

← All Scripting (Bash, Groovy) questions