Shell escape dollar signs

But allow for shell expansion using curly braces e.g. ${PWD}
This commit is contained in:
Stanko K.R
2023-03-22 16:57:17 +01:00
parent 60a19f0b30
commit 3ab16c8994
3 changed files with 31 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
module Mrsk::Utils
extend self
DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX = /\$(?!{[^\}]*\})/
# Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array).
def argumentize(argument, attributes, sensitive: false)
Array(attributes).flat_map do |key, value|
@@ -75,7 +77,9 @@ module Mrsk::Utils
# Escape a value to make it safe for shell use.
def escape_shell_value(value)
value.to_s.dump.gsub(/`/, '\\\\`')
value.to_s.dump
.gsub(/`/, '\\\\`')
.gsub(DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX, '\$')
end
# Abbreviate a git revhash for concise display