How to Use BashOperator in Airflow: Syntax and Example
Use
BashOperator in Airflow to run bash commands as tasks by importing it from airflow.operators.bash and defining it inside a DAG with the bash_command parameter. This operator executes the specified bash command when the task runs.Syntax
The BashOperator runs bash commands in Airflow tasks. You create it by specifying a task_id and a bash_command. The task_id is a unique name for the task, and bash_command is the shell command to execute.
python
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime default_args = { 'start_date': datetime(2024, 1, 1), } dag = DAG('example_bash_operator', default_args=default_args, schedule_interval='@daily') bash_task = BashOperator( task_id='run_bash_command', bash_command='echo "Hello from BashOperator"', dag=dag )
Example
This example shows a simple DAG with one BashOperator task that prints a message to the console. When the task runs, it executes the bash command echo "Hello from BashOperator".
python
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime default_args = { 'start_date': datetime(2024, 1, 1), } dag = DAG('bash_operator_example', default_args=default_args, schedule_interval='@once') bash_task = BashOperator( task_id='print_hello', bash_command='echo "Hello from BashOperator"', dag=dag ) bash_task
Output
[2024-01-01 00:00:00,000] {bash_operator.py:XXX} INFO - Running command: echo "Hello from BashOperator"
Hello from BashOperator
[2024-01-01 00:00:00,XXX] {bash_operator.py:XXX} INFO - Command exited with return code 0
Common Pitfalls
- Not setting
start_datein DAG'sdefault_argscauses scheduling errors. - Using shell commands that require environment variables without setting them can fail.
- Forgetting to import
BashOperatorfromairflow.operators.bashleads to import errors. - Passing complex commands without proper quoting or escaping may cause unexpected behavior.
python
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime default_args = { # Missing start_date causes errors } dag = DAG('bad_bash_operator', default_args=default_args, schedule_interval='@daily') # Wrong: missing quotes around command bash_task_wrong = BashOperator( task_id='bad_command', bash_command='echo Hello', dag=dag ) # Right: command as string with quotes bash_task_right = BashOperator( task_id='good_command', bash_command='echo Hello', dag=dag )
Quick Reference
| Parameter | Description |
|---|---|
| task_id | Unique identifier for the task |
| bash_command | The bash command string to execute |
| env | Optional dictionary of environment variables for the command |
| cwd | Optional working directory for the command |
| xcom_push | If True, pushes command output to XCom (default False) |
Key Takeaways
Use BashOperator to run bash commands as Airflow tasks by specifying task_id and bash_command.
Always set start_date in DAG default_args to avoid scheduling issues.
Quote bash commands properly to prevent syntax errors.
You can pass environment variables and working directory if needed.
Check task logs to see the output and status of bash commands.