Home > front end >  Remove from PATH variable any paths that contains a space
Remove from PATH variable any paths that contains a space

Time:11-12

I am having issues cross-compiling in WSL. The problem is that some of the windows paths that are added to the PATH variable contain spaces that cause errors in some Makefiles.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/TheDebianProject.DebianGNULinux_1.11.1.0_x64__76v4gfsz19hv4:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/tools/lxrunoffline:/mnt/d/pe_kit/Windows Performance Toolkit/:/mnt/d/pe_kit/Microsoft Application Virtualization/Sequencer/:/mnt/c/Users/IG-88/AppData/Local/Microsoft/WindowsApps

I need to remove any paths that contain a space. I know either sed or awk will probably do the job but I can't seem to do it myself. eg:

$ echo $PATH | some sed or awk command
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/tools/lxrunoffline:/mnt/c/Users/IG-88/AppData/Local/Microsoft/WindowsApps

CodePudding user response:

If you're using bash then you can do

PATH=$(IFS=':'; a=($PATH); IFS=' '; a=(${a[*]/* *}); IFS=':'; echo "${a[*]}")

Example:

myPATH="/win 1:/usr:/win 2:/usr/bin:/win 3:/usr/local/bin:/win 4"
(IFS=':'; a=($myPATH); IFS=' '; a=(${a[*]/* *}); IFS=':'; echo "${a[*]}")
/usr:/usr/bin:/usr/local/bin

CodePudding user response:

There's many approaches, I find this short and relatively readable:

PATH=$(grep -v '[[:space:]]'<<<"${PATH//:/$'\n'}"|paste -sd:) make

Edit: as per KamilCuk's suggestion.

For no Windows at all, replace the regex with '^/mnt/c/'. Simply PATH=${PATH%%:/mnt/c/*} make is also fine for this, in your case, and should be fine in the general case too I think. Unless you've appended something to PATH that you need in the build.

CodePudding user response:

Using sed

$ echo $PATH | sed 's|:/mnt/[a-z]/[a-z_]*\?/\?[A-Za-z]* [A-Za-z]* \?[A-Za-z]*\?[^:]*||g'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/tools/lxrunoffline:/mnt/c/Users/IG-88/AppData/Local/Microsoft/WindowsApps
  • Related