Skip to main content

How to Run a Bash Command with @task.bash in Apache Airflow®

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

@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​

Syntax
@task.bash
def <task name>(<parameters>) -> str:
return "<command>"
PartWhat it means
@task.bashTurns 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 its dags folder in your AIRFLOW_HOME.

Step 1: Write the Dag​

Create dags/greet_with_bash.py:

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()

The greet_with_bash.py Dag file in an editor window: a @task.bash function greet that takes name and returns the f-string echo "Hello, name!", called with "Ada".

  • greet returns a command; it doesn't run anything itself.
  • The f-string puts the name into the command, so greet("Ada") returns echo "Hello, Ada!".
  • -> str documents 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 log of the greet task, shown as @task.bash: Success, then Running command with /bin/bash -c and echo "Hello, Ada!", Output:, Hello, Ada!, and Command exited with return code 0.

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​

MistakeWhat happensDo this instead
Writing the command in the function without returnThe 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 commandThe guide lists return f'echo "here is the message: {message}"' under what not to do, because the command isn't escapedPass such values as environment variables with env

Troubleshooting​

SymptomLikely causeFix
TypeError: The returned value from the TaskFlow callable must be a non-empty string.The function returned nothing, or an empty stringMake sure every path through the function returns a command
Bash command failed. The command returned a non-zero exit code 1.The returned command failedRead 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​

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.