Urgent.News

What's breaking now, across thousands of outlets.

Tech

A double-quoted $HOME sent three of my directories nowhere I could find them

A double-quoted $HOME sent three of my directories nowhere I could find them I was calling into WSL from a PowerShell script, the way I do a dozen times a day: build a command string, hand it to wsl , let bash do the real work on the Linux side. Something like this, simplified down to the part that mattered: wsl -d Ubuntu-24.04 -- bash -c "mkdir -p \ $HOME /build && cd \ $HOME /build && ..." The…

A PowerShell script inadvertently sent three of the author's directories nowhere, as the double-quoted $HOME variable expanded prematurely. The author was using the WSL (Windows Subsystem for Linux) from a PowerShell script to run commands on the Linux side. The PowerShell command was structured as follows: `wsl -d Ubuntu-24.04 -- bash -c mkdir -p $HOME/build && cd $HOME/build && ...`.

The issue arose because PowerShell treats backslashes as escape characters, while PowerShell does not. So, when `$HOME` was placed inside a double-quoted PowerShell string, it expanded the variable right away, turning it into a literal backslash followed by the rest of the variable. This resulted in a Windows-style path without any separators, which Bash didn't recognize as a valid absolute path and treated it as a relative path instead.

As a result, the `mkdir -p` command created directories with mangled names in the current working directory, rather than the intended home directory. These directories were found later, scattered across different working directories, with names reflecting the broken Windows path. The author suspected that the Windows-style backslashes were stripped away during the transit through quoting and re-quoting processes before reaching the WSL command line.

The solution to the problem was straightforward: use single quotes in PowerShell. By encasing the variable within single quotes (`' $HOME/build '`), PowerShell would no longer interpret the $HOME as a variable, and Bash would resolve it against its own environment when it encountered the command. This change ensured that the directories were created correctly in the intended home directory.

The author emphasized that the failure wasn't immediately apparent, as the `mkdir -p` command executed successfully and the script continued running. The only indication of the issue came from the presence of oddly-named directories that were noticed later during a routine directory listing.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

git commit -m with a path argument doesn't commit what you staged

git commit -m "msg" -- doesn't commit what you staged I'd staged a handful of files, run a narrow git commit -m "fix: ..." -- path/to/file.rs , and assumed the diff that landed matched what I'd git…

  • git commit -m with path argument commits only current working-tree content
  • Overrides staged changes, disregarding files added with git add --cached
  • Verifying committed changes with git diff recommended in shared environments

Deleting files inside WSL doesn't shrink the disk they lived on

Deleting files inside WSL doesn't shrink the disk they lived on WSL2 keeps its entire Linux filesystem inside one file on the Windows side, a virtual hard disk with a .vhdx extension.

  • Files inside WSL do not shrink disk automatically.
  • Use fstrim, wsl --shutdown, and Optimize-VHD to manually reduce size.
  • Cleanup reduces size by 672GB in example scenario.

More from Thursday 3 September →