From 66c942ab08070ce292091d8d141a676db5d367b6 Mon Sep 17 00:00:00 2001 From: Optimierungswerfer <95424917+Optimierungswerfer@users.noreply.github.com> Date: Thu, 9 Dec 2021 13:27:45 +0100 Subject: [PATCH] Fix wrong order of parameter expansion Since some time (unfortunately, I could not figure out why or since when exactly) the script stopped working. It turned out that the $p variable had been empty inside the "cd" and "%my_app%" commands after being defined in the launch command. After some fiddling, it became clear that $p was being expanded too early and in the same context as the "bash -i -c" command before "bash -i -c" even started its work. This is solved by escaping the parameter expansion (as well as the sub-shells inside the launch command) once with a backslash. Also see the added comment part. --- wsl_nvim.bat | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wsl_nvim.bat b/wsl_nvim.bat index c36dd8c..af904f5 100644 --- a/wsl_nvim.bat +++ b/wsl_nvim.bat @@ -42,7 +42,15 @@ set pp=%pp:;=\;% :: full wt.exe path: %LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe :: GIANT GOTCHA! Can only strip outter double quotes from %pp% if placing within :: double quotes, else special chars will be interpretted literally, e.g. ^ will escape. -set launch="p=$(wslpath '%pp:"=%') && cd \\"^""$(dirname \\"^""$p\\"^"")\\"^"" && %my_app% \\"^""$p\\"^"" +:: Another GOTCHA! regarding the order of parameter expansion / sub-shell execution: +:: To use parameter expansion for the $p variable, the $ must be escaped using a backslash \$ +:: because the command is passed to bash via -c and hence would otherwise perform the expansion too early +:: in the context of the shell that is executing the "bash -c" command instead of passing it through +:: to the actual sub-shell. This resulted in $p being empty as it is only defined *inside* the sub-shell. +:: This is also true for the $(dirname "\$p") sub-shell call which must be escaped as \$() to avoid it being +:: executed before its argument "\$p" got expanded which would result in it outputting a dot, because it +:: would consume the literal string "\$p" as an argument, which is not a path. +set launch="p=$(wslpath '%pp:"=%') && cd \\"^""\$(dirname \\"^""\$p\\"^"")\\"^"" && %my_app% \\"^""\$p\\"^"" :: Use `start` to launch cmd and cleanup/close the parent process immediately. :: bash -i starts bash interactively.