Skip to main content
Inspiring
January 31, 2024
Question

Using io.popen to run an executable with a space in its path

  • January 31, 2024
  • 1 reply
  • 804 views

So I have an executable which I'm trying to run in a plugin using lua's io.popen. The path has a space in it and I can't for the life of me figure out how to escape the path. I've tried a variety of quotes, single and double and I've tried using gsub to escape the spaces - s = s:gsub( ' ', '\\') - but it's beibng stubborn. Any thoughts appreciated.

 

MacOs, btw.

This topic has been closed for replies.

1 reply

Inspiring
January 31, 2024

I believe that there is a missing spave after \\

I use this:

string.gsub(SourceDir, " ", "\\ ")

 

kimaldisAuthor
Inspiring
January 31, 2024

No, there's two backslashes there. I've tried 1,2,3,4 and single quotes. in the gsub  plus all variations of single and double quotes around the path.;.

kimaldisAuthor
Inspiring
January 31, 2024

I use this function to quote arguments that are to be passed to the platform's shell, properly handling the contents of the arguments that contain magic characters that need to be escaped:

--[[----------------------------------------------------------------------------
public string
q (string s)

Returns "s" quoted for the current platform's shell, with the contents of
"s" suitably escaped:

Mac: '    Windows: "
------------------------------------------------------------------------------]]

function Util.q (s)
    if MAC_ENV then 
        return "'" .. s:gsub ("'", [['\'']]) .. "'"
    else
        return '"' .. s:gsub ('[%%"]', {['%'] = '%%', ['"'] = '""'}) .. '"'
        end
    end

Thanks, John.