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.
So after some debugging using
bash -xand removing the output to/dev/nullit became apparent that whilencwas available in my bash, starting the script viabash script.shstarts a new shell, which does not load the alias I set in my~/.bashrcwhich wasalias nc='ncap'.This was obfuscated due to the Git Bash not loading the
.bashrcby default, as mentioned here and the fact that I was debugging the command by usingbash -xbefore, but did not know that piping a commands output to/dev/nulldoes also not show it in the debugging output. So in the end it just showedin a loop, which I suspected to be a successful execution.