How to Run a Bash Command with @task.bash in Apache Airflow®
@task.bash is the TaskFlow way to run a shell command in Apache Airflow®. You write a Python function that returns the command as text, and Airflow runs that text in bash when the task runs. Because the function is ordinary Python, it can build the command from its arguments first. This guide runs a greeting command built from a name on Airflow 3.4.0.
Syntax
@task.bash
def <task name>(<parameters>) -> str:
return "<command>"
| Part | What it means |
|---|---|
@task.bash | Turns the function into a task that runs the command the function returns |
<task name> | The function's name, which becomes the task's ID |
<parameters> | Values the function uses to build the command. You pass them when you call the task in the Dag |
return "<command>" | The command for bash to run, as a non-empty string |
The BashOperator guide says that with @task.bash, the command is "a non-empty string value returned from the decorated callable", and calls the decorator "the Taskflow equivalent of BashOperator".
The Example
A Dag called greet_with_bash has one task, greet, that takes a name and returns an echo command greeting it. The Dag calls it with "Ada".
Prerequisites
- Apache Airflow® running, for example with
airflow standalone, and itsdagsfolder in yourAIRFLOW_HOME.
Step 1: Write the Dag
Create dags/greet_with_bash.py:
from airflow.sdk import dag, task
@dag(schedule=None, tags=["demo"])
def greet_with_bash():
@task.bash
def greet(name: str) -> str:
return f'echo "Hello, {name}!"'
greet("Ada")
greet_with_bash()

greetreturns a command; it doesn't run anything itself.- The f-string puts the name into the command, so
greet("Ada")returnsecho "Hello, Ada!". -> strdocuments that the function returns text.
Step 2: Run It and Read the Log
Trigger the Dag and open the greet task's log (How to Read a Task Log in the Apache Airflow® UI shows where to click):

The task's operator shows as @task.bash. The Running command line shows the command the function returned, with Ada already in it. Airflow ran it with /bin/bash -c, logged its output, Hello, Ada!, and marked the task a success because the command exited with code 0. Everything after the function returns works as in BashOperator, including the temporary folder and the exit codes.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing the command in the function without return | The task fails with TypeError: The returned value from the TaskFlow callable must be a non-empty string. | Return the command |
| Putting values from the trigger form straight into the command | The guide lists return f'echo "here is the message: {message}"' under what not to do, because the command isn't escaped | Pass such values as environment variables with env |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: The returned value from the TaskFlow callable must be a non-empty string. | The function returned nothing, or an empty string | Make sure every path through the function returns a command |
Bash command failed. The command returned a non-zero exit code 1. | The returned command failed | Read the Running command line to see exactly what ran |
FAQ
@task.bash or BashOperator?
They run commands the same way. @task.bash suits commands you build in Python from arguments, as greet("Ada") did. BashOperator suits a fixed command written once.
What does the task pass on to the next task?
The last line the command prints. With the default XCom settings it's stored under return_value, so this task stored Hello, Ada!. How to Stop a Task from Pushing an XCom in Apache Airflow® with do_xcom_push=False shows the last-line rule on two tasks.
Conclusion
@task.bash turned greet into a task: called with "Ada", it returned echo "Hello, Ada!", and Airflow ran that command, logged Hello, Ada! and succeeded. Return the command as a non-empty string, and pass values from outside through env rather than into the command text.
Additional Resources
- BashOperator guide —
@task.bashand BashOperator side by side. - TaskFlow — how decorated tasks work.
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.
