Skip to main content

Fixed: BashOperator Runs ~/.bashrc When env Is Set in Apache Airflow®

· 14 min read
Jagdish Kumawat
Founder @ Dewiride

In Apache Airflow® 3.4.0, a BashOperator task that set env ran ~/.bashrc before its command: what the file printed showed up in the task log, and what it exported reached the command. The same task with append_env=True didn't. Here's how I tracked it down on macOS, why it happens, and two ways to stop it until the fix is released.

What I Was Doing​

I was checking BashOperator's env setting for a guide, on Airflow 3.4.0 with the standard provider 1.21.0, the version in Airflow's constraints file, started with airflow standalone on macOS. env gives the command a new environment that holds only the variables you list, plus a few that Airflow and bash add. A task with env={"CITY": "Lisbon"} printed the names of the variables its command could see, and the list had two that neither my env, Airflow nor bash sets. My own ~/.bashrc exports both.

To look at it on its own, without my own .bashrc, I made a home folder for the test, test-home, next to the dags folder, with a two-line .bashrc:

test-home/.bashrc
echo "Hello from .bashrc"
export FROM_BASHRC=yes

The first line prints a message, and the second creates a variable, FROM_BASHRC, that nothing else sets. Then I wrote a Dag that runs the same command in two tasks: env_only with env alone, and env_appended with append_env=True as well:

dags/bashrc_check.py
from pathlib import Path

from airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import dag

TEST_HOME = str(Path(__file__).parent.parent / "test-home")


@dag(schedule=None, tags=["demo"])
def bashrc_check():
BashOperator(
task_id="env_only",
bash_command='echo "FROM_BASHRC=$FROM_BASHRC"',
env={"HOME": TEST_HOME},
)
BashOperator(
task_id="env_appended",
bash_command='echo "FROM_BASHRC=$FROM_BASHRC"',
env={"HOME": TEST_HOME},
append_env=True,
)


bashrc_check()
  • TEST_HOME is the full path of the test-home folder. Path(__file__) is the Dag file's own path, and the two .parent steps lead from dags/bashrc_check.py to the folder that holds dags.
  • env={"HOME": TEST_HOME} sets HOME, the variable that tells bash where the home folder is, so bash looks for .bashrc in test-home, not in my real home folder.
  • append_env=True adds HOME to the environment Airflow runs with, instead of replacing it. How to Set Environment Variables for BashOperator in Apache Airflow® with env and append_env explains both settings.
  • Both commands print FROM_BASHRC= followed by the variable's value, which is empty when nothing has set it.

I triggered bashrc_check from the UI and opened each task's log.

What I Expected​

FROM_BASHRC= with nothing after it, from both tasks. BashOperator runs its command with bash -c, and a shell started that way isn't interactive. The bash manual lists ~/.bashrc among the files an interactive shell reads; a non-interactive shell reads only the file named in the BASH_ENV variable, and neither task sets it.

What I Got​

env_appended printed FROM_BASHRC=, as expected. env_only didn't:

env_only log (excerpt)
[2026-12-04 10:42:33] INFO - Running command: ['/bin/bash', '-c', 'echo "FROM_BASHRC=$FROM_BASHRC"']
[2026-12-04 10:42:33] INFO - Output:
[2026-12-04 10:42:33] INFO - Hello from .bashrc
[2026-12-04 10:42:33] INFO - FROM_BASHRC=yes
[2026-12-04 10:42:33] INFO - Command exited with return code 0

The log of the env_only task in the Airflow UI: after Running command and Output, line 7 reads Hello from .bashrc and line 8 reads FROM_BASHRC=yes, followed by Command exited with return code 0.

Bash had run test-home/.bashrc before the command: its message is in the task log, and the variable it exports reached the command. The env_appended log from the same run:

env_appended log (excerpt)
[2026-12-04 10:42:33] INFO - Running command: ['/bin/bash', '-c', 'echo "FROM_BASHRC=$FROM_BASHRC"']
[2026-12-04 10:42:33] INFO - Output:
[2026-12-04 10:42:33] INFO - FROM_BASHRC=
[2026-12-04 10:42:33] INFO - Command exited with return code 0

The log of the env_appended task from the same run: after Output, line 7 reads FROM_BASHRC= with nothing after it, followed by Command exited with return code 0.

It happened in every run, and it happened the same way with the standard provider 1.22.0, its newest release on 2026-12-04.

What I Checked​

What the bash manual says. A non-interactive shell does read ~/.bashrc in one case. From the INVOCATION section of man bash, for bash 3.2.57, the version macOS ships as /bin/bash:

Bash attempts to determine when it is being run by the remote shell daemon, usually rshd. If bash determines it is being run by rshd, it reads and executes commands from ~/.bashrc, if that file exists and is readable.

rshd is an old Unix service that runs commands sent from other computers over the network. Nothing in Airflow uses it, and the manual doesn't say how bash recognizes it, so I looked at what the command is given.

What the command's input is. Every command has a standard input, the place it reads typed input from. Another task printed whether that input is a socket, a two-way connection between programs:

The stdin_type task
BashOperator(
task_id="stdin_type",
bash_command='[ -S /dev/stdin ] && echo "stdin is a socket" || echo "stdin is not a socket"; echo "SHLVL=$SHLVL"',
env={"CITY": "Lisbon"},
do_xcom_push=False,
)
  • [ -S /dev/stdin ] is true when /dev/stdin, the command's standard input, is a socket. && runs the first echo when it's true, and || the second one when it isn't.
  • SHLVL is bash's shell level, which comes up again below.
  • do_xcom_push=False stops the task saving its last line as an XCom.
Output
stdin is a socket
SHLVL=1

The command's standard input was a socket. A command that reads from its input doesn't get an empty one either: in a task whose command was read -t 5 line; echo "read exit status: $?", the log shows Output: at 10:34:01.288 and read exit status: 1 at 10:34:06.295. read waited the full five seconds that -t 5 allows, and then failed.

Where the socket comes from. In Airflow 3, each task runs in its own process, started by a supervisor process, and the two talk over a socket. On macOS, the Task SDK starts the task process with os.posix_spawn and puts that socket in place of its standard input:

airflow/sdk/execution_time/supervisor.py (task-sdk 1.4.0, excerpt)
file_actions = [
(os.POSIX_SPAWN_DUP2, child_requests.fileno(), 0),
(os.POSIX_SPAWN_DUP2, child_stdout.fileno(), 1),
(os.POSIX_SPAWN_DUP2, child_stderr.fileno(), 2),
(os.POSIX_SPAWN_DUP2, child_logs.fileno(), 3),
]

POSIX_SPAWN_DUP2 copies the first file descriptor onto the second, and file descriptor 0 is standard input, so child_requests, one end of the socket, becomes the task's standard input. On other systems, the Task SDK starts the task without posix_spawn by default, and its _reopen_std_io_handles function connects the same socket to file descriptor 0 with os.dup2.

Where BashOperator's command gets its input. BashOperator runs its command through SubprocessHook, which starts it with Python's subprocess.Popen and no stdin argument:

airflow/providers/standard/hooks/subprocess.py (standard provider 1.21.0, excerpt)
self.sub_process = Popen(
command,
stdout=PIPE,
stderr=STDOUT,
cwd=cwd,
env=env if env or env == {} else os.environ,
start_new_session=True,
restore_signals=True,
)

The subprocess documentation says of stdin, stdout and stderr: "With the default settings of None, no redirection will occur." So /bin/bash -c keeps the task process's standard input: the supervisor's socket.

The same command outside Airflow. To check that the socket is what makes bash read .bashrc, I ran the same command from a short Python script, once with a socket as its standard input and once with an empty one, and the same environment both times:

stdin-check/socket_stdin.py
import socket
import subprocess
import sys

home = sys.argv[1]
command = ["/bin/bash", "-c", 'echo "FROM_BASHRC=$FROM_BASHRC"']
parent, child = socket.socketpair()
for label, stdin in (("a socket", child), ("subprocess.DEVNULL", subprocess.DEVNULL)):
result = subprocess.run(
command, stdin=stdin, env={"HOME": home}, capture_output=True, text=True
)
print(f"stdin is {label}:")
print(result.stdout, end="")
  • socket.socketpair() creates two connected sockets, as the supervisor does, and child becomes the command's standard input in the first run.
  • subprocess.DEVNULL gives the command an empty standard input in the second run.
  • env={"HOME": home} is the same one-variable environment as in the env_only task, with the home folder taken from the command line.

I ran it in the folder that holds test-home, with Airflow's virtual environment active:

Terminal
python stdin-check/socket_stdin.py "$PWD/test-home"
Output
stdin is a socket:
Hello from .bashrc
FROM_BASHRC=yes
stdin is subprocess.DEVNULL:
FROM_BASHRC=

Same command, same environment: with a socket as its input, bash read .bashrc, and with an empty input, it didn't.

Why append_env=True made a difference. The bash manual describes SHLVL as "Incremented by one each time an instance of bash is started." With env alone, the command's environment has no SHLVL, so its bash starts at 1. With append_env=True, the command gets Airflow's environment as well, which had SHLVL=1 because I started Airflow from a shell, so its bash runs at 2. Two tasks printed both values, with a home folder whose .bashrc holds only export FROM_BASHRC=yes:

TaskSettingsOutput
rc_env_onlyenv with HOMEFROM_BASHRC=yes SHLVL=1
rc_append_envenv with HOME, and append_env=TrueFROM_BASHRC= SHLVL=2

Their command was echo "FROM_BASHRC=$FROM_BASHRC SHLVL=$SHLVL". A third task, without env, also ran at SHLVL=2. The manual doesn't mention the shell level in the rshd case; in my runs, bash read .bashrc only at level 1.

Upstream. I reported it as apache/airflow#78120. A fix, apache/airflow#78123, adds stdin=DEVNULL to that Popen call, so the command gets an empty standard input. It was open and not yet merged on 2026-12-04.

Why It Happens​

Three things line up in Airflow 3.4.0:

  1. The Task SDK connects its supervisor socket to the task process's standard input.
  2. SubprocessHook starts BashOperator's /bin/bash -c command without a stdin of its own, so the command inherits that socket.
  3. Bash takes a non-interactive shell whose input is a socket for one started by rshd, and reads ~/.bashrc. In my runs, it did that only at shell level 1, which is where a command with env alone starts, because its environment has no SHLVL.

The Fix​

The real fix belongs in Airflow: with an empty standard input, bash has no reason to read .bashrc. Until a release includes it, either make ~/.bashrc stop in non-interactive shells, or keep Airflow's environment with append_env=True.

Option 1: Stop .bashrc in Non-Interactive Shells​

Add these four lines at the very top of ~/.bashrc in the home folder of the user that runs Airflow, which is your own when you start airflow standalone yourself:

~/.bashrc (top of the file)
case $- in
*i*) ;;
*) return ;;
esac
  • $- holds the options the shell is running with. The bash manual says "PS1 is set and $- includes i if bash is interactive", so the letter i is there only in an interactive shell.
  • case compares $- with each pattern in turn. *i* matches any text that contains an i, and ;; ends that branch without doing anything, so an interactive shell goes on to read the rest of the file.
  • * matches anything else, and return makes bash stop reading the file there.

The lines have to come first, because whatever is above them still runs. With the four lines below the echo line instead, the socket test above still printed Hello from .bashrc, while the export below them didn't run.

They also change more than Airflow: every non-interactive bash that reads .bashrc now stops at them. An interactive shell still reads the whole file: with the four lines at the top, bash -i printed Hello from .bashrc and saw FROM_BASHRC=yes.

Option 2: Keep Airflow's Environment with append_env=True​

If the command may see Airflow's environment as well as your variables, add append_env=True, as the env_appended task does:

dags/bashrc_check.py (excerpt)
BashOperator(
task_id="env_appended",
bash_command='echo "FROM_BASHRC=$FROM_BASHRC"',
env={"HOME": TEST_HOME},
append_env=True,
)

In my runs, the command then ran at SHLVL=2, and bash didn't read .bashrc. That depends on Airflow's own environment having SHLVL, which it had because I started Airflow from a shell. The env_only task shows what bash does with an environment that has no SHLVL, so Option 1 is the one that doesn't depend on how Airflow was started.

After the Fix​

I added the four lines at the top of test-home/.bashrc and triggered bashrc_check again. The env_only log:

env_only log (excerpt)
[2026-12-04 13:12:53] INFO - Running command: ['/bin/bash', '-c', 'echo "FROM_BASHRC=$FROM_BASHRC"']
[2026-12-04 13:12:53] INFO - Output:
[2026-12-04 13:12:53] INFO - FROM_BASHRC=
[2026-12-04 13:12:53] INFO - Command exited with return code 0

The log of the env_only task after the four lines were added at the top of test-home/.bashrc: after Output, line 7 reads FROM_BASHRC= with nothing after it, and Hello from .bashrc is gone.

No Hello from .bashrc, and FROM_BASHRC is empty: bash stopped reading .bashrc at the new lines. env_appended printed FROM_BASHRC=, as before.

Common Mistakes​

MistakeWhat happensDo this instead
Putting the four lines below other lines of .bashrcThe lines above them still run: Hello from .bashrc still appearedPut them at the very top of the file
Setting HOME in env to a folder that has a .bashrcBash reads that folder's .bashrc: that's how test-home/.bashrc ran in this testPut the four lines at the top of that .bashrc too, or use append_env=True

Troubleshooting​

SymptomLikely causeFix
Lines from your .bashrc in a BashOperator task's logThe task sets env without append_env=True, so bash read ~/.bashrcAdd the four lines at the top of ~/.bashrc, or set append_env=True
A variable you didn't put in env is in the command's environment~/.bashrc exports itThe same fix
The task's log still shows .bashrc output after the fixThe four lines aren't at the very top of the file, or they're in another user's .bashrcMove them to the top of the .bashrc in the home folder of the user that runs Airflow

FAQ​

Does it happen on Linux?​

I tested only on macOS, whose /bin/bash is bash 3.2.57. On every system, Airflow connects the supervisor socket to the task's standard input, as the code above shows, but I haven't checked a newer bash.

Does it happen without env?​

Not in my runs. A task without env gets Airflow's environment, and its command ran at SHLVL=2, like the one with append_env=True.

Conclusion​

With env alone, BashOperator's command in Airflow 3.4.0 ran at shell level 1 with the supervisor's socket as its standard input, and bash read ~/.bashrc: Hello from .bashrc and FROM_BASHRC=yes showed up in the log. Four lines at the top of .bashrc stopped it, and append_env=True avoided it in my setup. The fix that gives the command an empty standard input is in apache/airflow#78123.

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.