Bash: Creating a shell variable in a bash script that I can access from command line
Categories:
Creating and Accessing Bash Variables from the Command Line

Learn how to define shell variables within Bash scripts and effectively access their values from your command-line interface, enabling dynamic script interaction.
Bash scripts are powerful tools for automating tasks in Linux and Unix-like environments. Often, you'll need to define variables within these scripts to store temporary data, configuration settings, or results of operations. A common requirement is to then access or utilize these variables from the command line after the script has finished executing. This article will guide you through the methods to achieve this, focusing on the nuances of shell environments and variable scope.
Understanding Variable Scope in Bash
Before diving into solutions, it's crucial to understand how Bash handles variable scope. By default, variables defined within a script are local to that script's execution environment. This means that once the script finishes, its process terminates, and all its local variables are destroyed. They are not automatically inherited by the parent shell (the command line you're working in).
Consider a simple script my_script.sh
:
#!/bin/bash
MY_VARIABLE="Hello from script"
echo "Inside script: $MY_VARIABLE"
If you run this script using ./my_script.sh
, you'll see the output "Inside script: Hello from script". However, if you then try to echo $MY_VARIABLE
in your terminal, you'll find it's empty. This is because MY_VARIABLE
was local to the script's subshell.
flowchart TD A[Parent Shell] --> B{Execute Script (Subshell)} B --> C[Script Defines MY_VARIABLE] C --> D[Script Uses MY_VARIABLE] D -- Script Exits --> B B -- Subshell Terminates --> A A -- MY_VARIABLE not available --> E[Parent Shell: MY_VARIABLE is undefined]
Default variable scope behavior in Bash
Method 1: Sourcing the Script
The most direct way to make variables defined in a script available in the current shell is to source the script. When you source a script (using source script_name
or . script_name
), it executes the commands within the script in the current shell environment, rather than launching a new subshell. This means any variables defined or modified in the script will persist in your current terminal session.
#!/bin/bash
EXPORTED_VAR="This variable is now in your shell"
ANOTHER_VAR="This one too!"
echo "Script sourced. Variables defined."
To use this, execute it like so:
source ./source_script.sh
echo $EXPORTED_VAR
echo $ANOTHER_VAR
.bashrc
, .profile
). Be cautious, as sourcing can also execute any other commands in the script directly in your current shell.Method 2: Exporting Variables
While sourcing makes variables available, sometimes you need a variable to be available not just in the current shell, but also in any subsequent processes or scripts launched from that shell. This is where the export
command comes in. When you export
a variable, it becomes an environment variable and is automatically passed down to any child processes.
#!/bin/bash
export SCRIPT_RESULT="Operation completed successfully"
LOCAL_VAR="This will not be exported"
echo "Inside script: SCRIPT_RESULT=$SCRIPT_RESULT"
If you run export_script.sh
normally (e.g., ./export_script.sh
), SCRIPT_RESULT
will not be available in your parent shell. This is because the export
command only affects the subshell where the script runs and its children. To make SCRIPT_RESULT
available in the parent shell, you still need to combine export
with source
:
source ./export_script.sh
echo $SCRIPT_RESULT
echo $LOCAL_VAR
You'll see SCRIPT_RESULT
printed, but LOCAL_VAR
will be empty. This demonstrates that export
marks a variable for inheritance, and source
ensures the definition happens in the current shell.
flowchart TD A[Parent Shell] --> B{Source Script (Current Shell)} B --> C[Script Defines and EXPORTs MY_VAR] C --> D[MY_VAR becomes Environment Variable] D --> E[Parent Shell: MY_VAR is available] E --> F{Launch New Process (Subshell)} F --> G[New Process inherits MY_VAR]
Variable availability with 'source' and 'export'
Method 3: Capturing Script Output
For cases where a script calculates a single value or a small set of values that you want to use in the command line, you can have the script print the value to standard output and then capture that output into a variable in your current shell. This is often used for functions or scripts that return a specific result.
#!/bin/bash
# Simulate some computation
RESULT_VALUE=$(( $(date +%s) % 100 ))
echo "The computed value is: $RESULT_VALUE"
# The value we want to capture
echo $RESULT_VALUE
To capture the last echo
'd value from the script, you can use command substitution:
MY_CAPTURED_VAR=$(./output_script.sh | tail -n 1)
echo "Captured from command line: $MY_CAPTURED_VAR"
tail -n 1
for the last line, or grep
for specific patterns). Ensure your script only outputs the desired value on a single line for easy capture.Choosing the Right Method
The best method depends on your specific use case:
- Sourcing (
source
or.
): Use when you want the script to directly modify the current shell's environment, such as setting up paths, aliases, or environment variables for the current session. This is common for configuration scripts. - Exporting (
export
withsource
): Use when you need variables to be available not only in the current shell but also in any subsequent child processes launched from it. This is crucial for environment variables that programs might read. - Capturing Output (
$(...)
): Use when a script's primary purpose is to compute and return a single value or a structured output that can be easily parsed. This is good for functions or utilities that provide a result.
export
are local to the current shell or script. Variables set with export
are environment variables and are inherited by child processes.