This works perfectly on CentOS and Fedora but always fails with Ubuntu. I found out that the SSH connection uses Dash shell rather than Bash. How do I make knife use Bash?
I tried you way but it still fails due to the connection using Dash Shell. I have an airgapped environment so I had to create a bootstrap script. I chose bash as it seems very popular. Running the bootstrap script locally works when run in the Bash shell.