escapeshellarg

function escapeshellarg( str ) --> esc

Description

Adds double quotes around a string and quotes/escapes any existing double quotes allowing you to pass a string directly to a shell function and having it be treated as a single safe argument. This function should be used to escape individual arguments to shell functions coming from user input.

Parameters

str

The string that will be escaped into a safe shell argument.

Return Values

esc

The escaped string.

Code

--ZFUNC-escapeshellarg-v1
local function escapeshellarg( str ) --> esc
   return '"'..str:gsub( '"', '\\"' )..'"'
end

return escapeshellarg

Examples

local t = require( "tapered" )
local escapeshellarg = require( "escapeshellarg" )

t.is( [["Hello's world"]], escapeshellarg( "Hello's world" ) )
t.is( [["use a \" to mark"]], escapeshellarg( [[use a " to mark]] ) )

t.done()

Inspired by