Challenge - 5 Problems
BashOperator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of BashOperator with simple echo command
What will be the output logged by the BashOperator when running this task?
Apache Airflow
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime dag = DAG('test_dag', start_date=datetime(2024, 1, 1)) task = BashOperator( task_id='print_hello', bash_command='echo Hello Airflow', dag=dag )
Attempts:
2 left
💡 Hint
Think about what the echo command does in a shell.
✗ Incorrect
The BashOperator runs the bash_command string in a shell. The command 'echo Hello Airflow' prints 'Hello Airflow' to the output.
❓ Configuration
intermediate2:00remaining
Correct way to pass multiple commands in BashOperator
Which option correctly runs two shell commands sequentially in a single BashOperator?
Attempts:
2 left
💡 Hint
In shell, commands separated by semicolon run sequentially.
✗ Incorrect
Using a semicolon ';' runs commands one after another. '&&' runs second only if first succeeds. The pipe '|' sends output of first to input of second, which is not correct here. Passing a list is invalid for bash_command.
❓ Troubleshoot
advanced2:00remaining
Error caused by incorrect bash_command syntax
What error will occur when running this BashOperator?
bash_command='echo Hello &&& echo World'
Attempts:
2 left
💡 Hint
Consider how the shell interprets '&&&'.
✗ Incorrect
The shell does not recognize '&&&' as a valid operator, so the BashOperator task will fail with a shell syntax error during execution.
🔀 Workflow
advanced2:00remaining
BashOperator task dependencies in Airflow DAG
Given two BashOperator tasks, task1 and task2, how do you set task2 to run only after task1 completes successfully?
Apache Airflow
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime dag = DAG('dependency_dag', start_date=datetime(2024, 1, 1)) task1 = BashOperator(task_id='task1', bash_command='echo Task 1', dag=dag) task2 = BashOperator(task_id='task2', bash_command='echo Task 2', dag=dag)
Attempts:
2 left
💡 Hint
Airflow uses bitshift operators or methods to set dependencies.
✗ Incorrect
Both 'task1 >> task2' and 'task1.set_downstream(task2)' set task2 to run after task1. 'task2 >> task1' reverses the order.
✅ Best Practice
expert2:00remaining
Best practice for passing dynamic parameters to BashOperator
Which method is best to pass a dynamic date string to a BashOperator command in Airflow?
Attempts:
2 left
💡 Hint
Airflow supports templating in operators for dynamic values.
✗ Incorrect
Using Jinja templating like '{{ ds }}' allows Airflow to inject the execution date dynamically at runtime, which is best practice for dynamic parameters.