How to Set Environment Variables for BashOperator in Apache Airflow® with env and append_env
BashOperator's env parameter hands variables to the shell command it runs. It's the safe way to get values into a command in Apache Airflow®. By default, though, env replaces the whole environment, so variables like PATH and USER no longer reach the command. This guide shows both effects on Airflow 3.4.0, and how append_env=True keeps the environment.
Syntax
BashOperator(
task_id="<task id>",
bash_command='<command that reads $<NAME>>',
env={"<NAME>": "<value>"},
append_env=<True or False>,
)
| Part | What it means |
|---|---|
env={"<NAME>": "<value>"} | The variables to give the command. Names and values are strings |
$<NAME> | How the command reads a variable: bash replaces it with the value when it runs |
append_env=<True or False> | False, the default: the command gets env instead of the environment Airflow runs in. True: it gets that environment with env added |
The BashOperator reference says the variables in env "are used instead of inheriting the current process environment", and that with append_env left at False, the operator "does not inherit the current process environment".
The Example
A Dag called bash_env has two BashOperator tasks that run the same command, echo "CITY=$CITY USER=$USER", and pass CITY in env. env_only passes nothing else, and env_appended adds append_env=True. USER, the name of the user Airflow runs as, is set in every terminal session, so it shows whether the rest of the environment reached the command.
Prerequisites
- Apache Airflow® running, for example with
airflow standalone, and itsdagsfolder in yourAIRFLOW_HOME.
Step 1: Write the Dag
Create dags/bash_env.py:
from airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import dag
@dag(schedule=None, tags=["demo"])
def bash_env():
BashOperator(
task_id="env_only",
bash_command='echo "CITY=$CITY USER=$USER"',
env={"CITY": "Lisbon"},
)
BashOperator(
task_id="env_appended",
bash_command='echo "CITY=$CITY USER=$USER"',
env={"CITY": "Lisbon"},
append_env=True,
)
bash_env()

env={"CITY": "Lisbon"}gives the command one variable,CITY.- The command is in single quotes in Python, so
$CITYand$USERreach bash as typed, and bash fills them in when it runs the command. append_env=Trueis the only difference between the two tasks.
Step 2: Read the Log of env_only
Trigger the Dag and open the env_only task's log (How to Read a Task Log in the Apache Airflow® UI shows where to click):

CITY=Lisbon USER=: CITY arrived, and USER is empty. env replaced the environment. In a check task that listed the names of all its variables, a command with env={"CITY": "Lisbon"} had only these:
CITY, fromenv;- six
AIRFLOW_CTX_variables that Airflow adds by itself, such asAIRFLOW_CTX_DAG_IDandAIRFLOW_CTX_TASK_ID; PWD,SHLVLand_, which bash sets itself.
There was no PATH either, so bash used its own default. On macOS that was /usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.: programs installed anywhere else, such as with Homebrew or in a virtual environment, aren't found.
With env alone, bash on macOS also runs ~/.bashrc before the command, so anything that file prints shows up in the log. Fixed: BashOperator Runs ~/.bashrc When env Is Set in Apache Airflow® explains why, and what to do.
Step 3: Read the Log of env_appended
Open the env_appended task's log:

CITY=Lisbon USER=you: with append_env=True, the command got the environment Airflow runs in, with CITY added, so USER holds the user's name. The screenshot shows it as you. PATH is kept too: a task that ran uv --version this way printed uv 0.13.10.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using env alone for a command that runs a program from outside the system folders, such as uv --version | The task fails: /bin/bash: uv: command not found, then Bash command failed. The command returned a non-zero exit code 127. | Add append_env=True |
A value that isn't a string: env={"COUNT": 3} | The task fails with TypeError: expected str, bytes or os.PathLike object, not int | Write the value as a string: {"COUNT": "3"} |
| Writing a value from the trigger form straight into the command | The guide warns that "escaping and sanitization of the Bash command is not performed" | Pass the value in env, as the guide says: "you should pass this via the env kwarg and use double-quotes inside the Bash command" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
command not found and exit code 127 | env without append_env=True left the command with bash's default PATH | Add append_env=True |
| A variable you set in your terminal is empty in the command | env replaced the environment | Add append_env=True, or pass the variable in env |
Lines from your ~/.bashrc in the task log | Bash ran ~/.bashrc because the task has env | See Fixed: BashOperator Runs ~/.bashrc When env Is Set in Apache Airflow® |
FAQ
What are the AIRFLOW_CTX_ variables?
Values Airflow adds to every BashOperator command's environment, even with env alone. The check listed six: AIRFLOW_CTX_DAG_ID, AIRFLOW_CTX_DAG_OWNER, AIRFLOW_CTX_DAG_RUN_ID, AIRFLOW_CTX_LOGICAL_DATE, AIRFLOW_CTX_TASK_ID and AIRFLOW_CTX_TRY_NUMBER. A command can read them to find out which Dag, run and task it belongs to.
Does @task.bash take env too?
Yes. The guide's own example passes it to the decorator: @task.bash(env={"message": ...}).
Conclusion
env={"CITY": "Lisbon"} gave both tasks CITY. On its own, it replaced the environment: USER came out empty, and PATH fell back to bash's default, so uv wasn't found. With append_env=True, the command kept Airflow's environment and got CITY on top. Use append_env=True unless the command must run with nothing but the variables you pass.
Additional Resources
- BashOperator guide —
envand safe use of values from the trigger form. - BashOperator reference —
env,append_envand the other parameters.
Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.
