0
0
Apache Airflowdevops~20 mins

BashOperator for shell commands in Apache Airflow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BashOperator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
)
AHello Airflow
Becho Hello Airflow
Cbash_command executed
DTask failed with error
Attempts:
2 left
💡 Hint
Think about what the echo command does in a shell.
Configuration
intermediate
2:00remaining
Correct way to pass multiple commands in BashOperator
Which option correctly runs two shell commands sequentially in a single BashOperator?
Abash_command='echo Start && echo End'
Bbash_command=['echo Start', 'echo End']
Cbash_command='echo Start | echo End'
Dbash_command='echo Start; echo End'
Attempts:
2 left
💡 Hint
In shell, commands separated by semicolon run sequentially.
Troubleshoot
advanced
2:00remaining
Error caused by incorrect bash_command syntax
What error will occur when running this BashOperator? bash_command='echo Hello &&& echo World'
ABashOperator task fails with shell syntax error
BSyntaxError in Python
CTask runs successfully printing 'Hello &&& echo World'
DAirflow DAG fails to parse
Attempts:
2 left
💡 Hint
Consider how the shell interprets '&&&'.
🔀 Workflow
advanced
2: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)
Atask2 >> task1
BBoth C and D
Ctask1.set_downstream(task2)
Dtask1 >> task2
Attempts:
2 left
💡 Hint
Airflow uses bitshift operators or methods to set dependencies.
Best Practice
expert
2: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?
AHardcode the date string in bash_command
BConcatenate date string in Python before passing to bash_command
CUse Jinja templating with '{{ ds }}' in bash_command
DUse environment variables set outside Airflow
Attempts:
2 left
💡 Hint
Airflow supports templating in operators for dynamic values.