The BashOperator is one of the most commonly used operators in Airflow. It executes bash commands or a bash script from within your Airflow DAG.
In this guide you’ll learn:
@task.bash decorator.To get the most out of this guide, you should have an understanding of:
@task.bash decoratorThe BashOperator is part of core Airflow and can be used to execute a single bash command, a set of bash commands, or a bash script ending in .sh. The @task.bash decorator can be used to create bash statements using Python functions and is available as of Airflow 2.9.
The following parameters can be provided to the operator and decorator:
bash_command: Defines a single bash command, a set of commands, or a bash script to execute. This parameter is required.env: Defines environment variables in a dictionary for the bash process. By default, the defined dictionary overwrites all existing environment variables in your Airflow environment, including those not defined in the provided dictionary. To change this behavior, you can set the append_env parameter. If you leave this parameter blank, the BashOperator inherits the environment variables from your Airflow environment.append_env: Changes the behavior of the env parameter. If you set this to True, the environment variables you define in env are appended to existing environment variables instead of overwriting them. The default is False.output_encoding: Defines the output encoding of the bash command. The default is utf-8.skip_exit_code: Defines which bash exit code should cause the BashOperator to enter a skipped state. The default is 99.cwd: Changes the working directory where the bash command is run. The default is None and the bash command runs in a temporary directory.The behavior of a BashOperator task is based on the status of the bash shell:
skip_exit_code).If you expect a non-zero exit from a sub-command you can add the prefix set -e; to your bash command to make sure that the exit is captured as a task failure.
Both the bash_command and the env parameter can accept Jinja templates. However, the input given through Jinja templates to bash_command is not escaped or sanitized. If you are concerned about potentially harmful user input you can use the setup shown in the BashOperator documentation.
The following are common use cases for the BashOperator and @task.bash decorator in Airflow DAGs:
In Airflow 2.9+, you can use @task.bash to create bash statements using Python functions. This decorator is especially useful when you want to run bash commands based on complex Python logic, including inputs from upstream tasks. The following example demonstrates how to use the @task.bash decorator to conditionally run different bash commands based on the output of an upstream task.
The BashOperator can execute any number of bash commands separated by &&.
In this example, you run two bash commands in a single task:
echo Hello $MY_NAME! prints the environment variable MY_NAME to the console.echo $A_LARGE_NUMBER | rev 2>&1 | tee $AIRFLOW_HOME/include/my_secret_number.txt takes the environment variable A_LARGE_NUMBER, pipes it to the rev command which reverses any input, and saves the result in a file called my_secret_number.txt located in the /include directory. The reversed number will also be printed to the console.The second command uses an environment variable from the Airflow environment, AIRFLOW_HOME. This is only possible because append_env is set to True.
It is also possible to use two separate BashOperators to run the two commands, which can be useful if you want to assign different dependencies to the tasks.
The BashOperator can also be provided with a bash script (ending in .sh) to be executed.
For this example, you run a bash script which iterates over all files in the /include folder and prints their names to the console.
Make sure that your bash script (my_bash_script.sh in this example) is available to your Airflow environment. If you use the Astro CLI, you can make this file accessible to Airflow by placing it in the /include directory of your Astro project.
It is important to make the bash script executable by running the following command before making the script available to your Airflow environment:
If you use the Astro CLI, you can run this command before running astro dev start, or you can add the command to your project’s Dockerfile with the following RUN command:
Astronomer recommends running this command in your Dockerfile for production builds such as Astro Deployments or in production CI/CD pipelines.
After making the script available to Airflow, you only have to provide the path to the script in the bash_command parameter. Be sure to add a space character at the end of the filepath, or else the task will fail with a Jinja exception!
Using the BashOperator is a straightforward way to run a script in a non-Python programming language in Airflow. You can run a script in any language that can be run with a bash command.
In this example, you run some JavaScript to query a public API providing the current location of the international Space Station. The query result is pushed to XCom so that a second task can extract the latitude and longitude information in a script written in R and print the data to the console.
The following setup is required:
If you use the Astro CLI, the programming language packages can be installed at the OS level by adding them to the packages.txt file of your Astro project.
The following JavaScript file contains code for sending a GET request to the /iss-now path at api.open-notify.org and returning the results to stdout, which will both be printed to the console and pushed to XCom by the BashOperator.
The second task runs a script written in R that uses a regex to filter and print the longitude and latitude information from the API response.
To run these scripts using the BashOperator, ensure that they are accessible to your Airflow environment. If you use the Astro CLI, you can place these files in the /include directory of your Astro project.
The DAG uses the BashOperator to execute both files defined above sequentially.