How to Run a Bash Command in Apache Airflow® with BashOperator
Plenty of pipeline steps are already shell commands: compress a file, run a script, call a command-line tool. In Apache Airflow®, BashOperator runs one as a task. Airflow starts bash with your command, writes everything it prints to the task log, and uses its exit code to decide whether the task succeeded. This guide runs one echo command on Airflow 3.4.0 and reads its log.
Syntax
from airflow.providers.standard.operators.bash import BashOperator
BashOperator(
task_id="<task id>",
bash_command="<command>",
)
| Part | What it means |
|---|---|
from airflow.providers.standard.operators.bash import BashOperator | The operator, from the standard provider. It's installed with Airflow: apache-airflow-core requires it |
task_id="<task id>" | The task's name in the Dag |
bash_command="<command>" | The command, or several separated by ;, that bash runs |
The BashOperator guide says: "Use the BashOperator to execute commands in a Bash shell." The operator's own documentation describes the result: "a non-zero exit code will result in task failure and zero will result in task success."
The Example
A Dag called bash_hello has one task, say_hello, that runs echo "Hello from Bash".
Prerequisites
- Apache Airflow® running, for example with
airflow standalone, and itsdagsfolder in yourAIRFLOW_HOME.
Step 1: Write the Dag
Create dags/bash_hello.py:
from airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import dag
@dag(schedule=None, tags=["demo"])
def bash_hello():
BashOperator(
task_id="say_hello",
bash_command='echo "Hello from Bash"',
)
bash_hello()

- The operator is created inside the Dag function, which makes it a task of that Dag.
- The command is in single quotes in Python, so the double quotes inside it reach bash unchanged.
Step 2: Run It and Read the Log
Trigger the Dag and open the say_hello task's log (How to Read a Task Log in the Apache Airflow® UI shows where to click):

The task succeeded, and the log shows each stage of the command:
Tmp dir root locationis the folder the command ran in. By default it's a new temporary folder: the operator's documentation says "If None (default), the command is run in a temporary directory." Passcwdto choose another.Running command: ['/bin/bash', '-c', 'echo "Hello from Bash"']shows how Airflow started it:/bin/bash -cfollowed by your command.- After
Output:comes everything the command printed:Hello from Bash. Command exited with return code 0is the exit code. 0 means success.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A command whose last step succeeds after an earlier one failed, such as false; echo done | The task succeeds: only the exit code of the whole command counts | Start it with set -e;. set -e; false; echo done failed the task with Bash command failed. The command returned a non-zero exit code 1. |
Importing it from Airflow 2's path, airflow.operators.bash | It still works in 3.4.0, with a warning: The `airflow.operators.bash.BashOperator` attribute is deprecated. | Import it from airflow.providers.standard.operators.bash |
| Putting values from the trigger form into the command with a template | The documentation warns that the operator "does not perform any escaping or sanitization of the command" | Pass them as environment variables with env, and quote the variable in the command |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Bash command failed. The command returned a non-zero exit code 1. | The command ended with exit code 1 | Read the lines after Output: for the command's own error |
| The task ends as skipped | The command exited with code 99 | Use another exit code for failures, or set skip_on_exit_code |
| A file the command writes isn't where you expected | It ran in a temporary folder | Use full paths, or set cwd |
FAQ
Which exit codes fail the task?
Every code except 0 and 99. 0 is success, and 99 leaves the task skipped: a task running exit 99 logged Bash command returned exit code 99. Skipping. and ended as skipped. The skip_on_exit_code parameter changes which code skips; the operator's documentation says that with skip_on_exit_code=None, "any non-zero exit code will be treated as a failure."
Is there a TaskFlow version?
Yes: @task.bash runs the command your function returns. The guide calls it "the Taskflow equivalent of BashOperator".
Conclusion
BashOperator(task_id="say_hello", bash_command='echo "Hello from Bash"') ran the command with /bin/bash -c in a temporary folder, wrote Hello from Bash to the task log, and succeeded with exit code 0. Start multi-step commands with set -e; so a failure in the middle fails the task.
Additional Resources
- BashOperator guide — templating, skipping and running scripts.
- BashOperator reference — every parameter, including
cwd,envandskip_on_exit_code.
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.
