Git Bash not exiting while loop, while bash in ubuntu does

45 Views Asked by At

I got a while loop in a Bash script which looks something like this

#!/bin/bash

# do something with kubectl port-forward

while ! nc -vz localhost "$port" > /dev/null 2>&1 ; do
    # echo sleeping
    sleep 0.1
done

#access ressource on the port-forwarded website

which basically is supposed to be waiting until a kubectl port-forward command has succesfully started.

However, executing the script under Git Bash (I execute via MSYS_NO_PATHCONV=1 bash) leads to an infinite loop, as it never exits the while loop. I installed ncap and the nc command nc -vz localhost $port works on its own fine. Executing with -x shows that it is looping over the nc command which is running successfully.

Executing the same script in Ubuntu WSL, it just works, exits the while loop as intended and continues.

1

There are 1 best solutions below

0
A7exSchin On

So after some debugging using bash -x and removing the output to /dev/null it became apparent that while nc was available in my bash, starting the script via bash script.sh starts a new shell, which does not load the alias I set in my ~/.bashrc which was alias nc='ncap'.

This was obfuscated due to the Git Bash not loading the .bashrc by default, as mentioned here and the fact that I was debugging the command by using bash -x before, but did not know that piping a commands output to /dev/null does also not show it in the debugging output. So in the end it just showed

nc -vz localhost 8002
sleep 0.1

in a loop, which I suspected to be a successful execution.