WSL — Convert Windows Path to Linux Path in Shell

Maciej
Oct 5, 2021

Why we do this ?

Because the volume parameter of docker-compose only accepts WSL format

How do this ?

The test command realized by the sed command is as follows.

$ echo "c:/Project/test" \
| sed -E 's/^(\w+)+/\L\1/g' \
| sed -e 's/\\/\//g' \
| sed -e 's/://g' \
| sed -e '/^\//!s/^/\//g'
/c/Temp/test

Explanation:

  • sed -E ‘s/^(\w+)+/\L\1/g’ → Lowercase initial letters
  • sed -e ‘s/\\/\//g’ → Convert [] to [/]
  • sed -e ‘s/://’ → Remove [:]
  • sed -e ‘/^\//!s/^/\//g’ → If it does not start with [/], add [/] at the beginning

How use this ?

If we run this the output should be like below:

$ sh wsl.sh
/c/Temp/test

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.