At the prompt, a script may receive a value typed by the user or capture output from a command. A shell variable gives that value a name the script can use later.
In this piece, I’ll show how to assign and use Bash variables, including passing a value to a child process.
TL;DR
In Bash, assign a value with no spaces around =, then use $name to expand it and quote expansions that may contain spaces.
- Use name=”Ada Lovelace” to assign a string.
- Use “$name” when passing the value to a command.
- Use export name when a child process must inherit it.
What Are Shell Script Variables?
A Bash variable is a named value. A script can reuse it instead of repeating fixed text.
Bash variable names start with a letter or underscore and are case-sensitive. Uppercase names often mark environment settings by convention, but Bash does not require them.
project="shell-demo"
printf '%s\n' "$project"
printf '%s\n' '$project'
Single quotes preserve literal characters, so the second printf prints the dollar sign and name. I chose Bash for this named-file run because the piece covers Bash-specific variable syntax. The program file and its terminal capture use the same source command.
How to Assign and Use Bash Variables
Values can be assigned directly or populated while the script runs.
Step 1: Assign a value without spaces around equals
Leave the dollar sign off the assignment name. Quote a value that contains spaces.
name="Ada Lovelace"
report_path="/home/ada/Monthly Reports/report.txt"
Step 2: Expand and join values safely
Prefix a variable with $ to read it. Use braces when another character immediately follows the name, because braces mark where the variable name ends.
first="Ada"
last="Lovelace"
full_name="${first} ${last}"
printf 'Name: %s\n' "$full_name"
prefix="monthly"
file="${prefix}_report.txt"
Step 3: Capture input or command output
The read builtin stores a line typed by the user. Command substitution, written as $(command), runs a command and stores its standard output after removing trailing newline characters.
read -r -p 'Enter a project name: ' project
current_dir="$(pwd)"
printf 'Project: %s\nDirectory: %s\n' "$project" "$current_dir"
The -r option tells read to treat backslashes literally, a useful choice for ordinary text input.
Step 4: Export a value to a child process
Use export when a command or script started from this shell needs a value in its environment.
export PROJECT="shell-demo"
bash -c 'printf "Child sees PROJECT=%s\\n" "$PROJECT"'
A one-command assignment such as PROJECT=”shell-demo” command applies the value only for that invocation.
Step 5: Use arguments and arithmetic
Positional parameters provide access to values supplied after the script name.
printf 'Count: %s\n' "$#"
printf 'First argument: %s\n' "$1"
total=$((7 + 5))
printf 'Total: %s\n' "$total"
For example, running bash task.sh “hello world” gives one argument, not two, because the shell passes the quoted phrase as one value. The Bash manual documents positional parameters and the arithmetic expansion syntax.
Variable Scope and Common Mistakes
Variable errors often come from assignment syntax, unquoted expansion or assuming a child script can change its parent. Check these cases before debugging the command that consumes the value.
| Case | What happens | Safer form |
|---|---|---|
| Spaces around = | Bash tries to run a command | name=”Ada Lovelace” |
| Unquoted expansion | Whitespace can split the value into arguments | command “$name” |
| Unset variable | Usually expands to an empty string | Check with [[ -z “$name” ]] |
| Child assignment | Does not change the parent shell | Export before launching the child |
| Empty versus unset | Both may expand to empty text | Use [[ -v name ]] to test whether it is set |
Use [[ -z “$name” ]] to test whether the value is empty. Use [[ -v name ]] to distinguish an unset variable from one set to an empty string.
Unset removes the name. Assigning an empty string leaves it set with no characters. Use readonly name when a script should not change its value later.
display_name="${name:-Guest}"
With :-, display_name receives Guest when name is missing or empty. The related := form also assigns that fallback to name itself.
Name: Ada Lovelace
Label: Engineer: Ada Lovelace
Child sees PROJECT=shell-demo
Argument count: 1
First argument: hello world
Sum: 12
Unset value length: 0

The value “hello world” arrives as one argument. The child Bash process receives PROJECT because it was exported.
Conclusion: Keep Expansion Explicit
Trace each value from where a script receives it to the command that consumes it. That question helps locate a broken assignment or an unexpected process boundary.
For the next step, see how to run a shell file and the chmod guide when executing a script directly. Use the guide to set environment variables in Linux when a value should be available beyond one script invocation.
For output formatting, see the Linux echo command guide. If a script depends on an executable path, find its location with which.
FAQ
These quick answers cover the assignment and scope questions that come up when a script first uses variables.
How do you assign a variable in a Bash script?
Write the variable name, an equals sign and its value with no spaces around the equals sign, such as name=”Ada”.
How do you pass a variable to another script?
Use export name before starting the child script, or set the value for one command with name=value command. A child process cannot change the parent shell’s variable.
How do you check whether a Bash variable is empty?
Use [[ -z “$name” ]] to test whether its value is empty. Use [[ -v name ]] to check whether the variable is set, including when its value is an empty string.
