git.iko.soy/typeable/nixpkgs
on master help sign in
lib: Make escapeShellArg more robust
df47509aszlig
lib/strings.nix+7−8
@@ -203,20 +203,19 @@ rec {
203 */
203 */
204 escape = list: replaceChars list (map (c: "\\${c}") list);
204 escape = list: replaceChars list (map (c: "\\${c}") list);
205
205
206− /* Escape all characters that have special meaning in the Bourne shell.
206+ /* Quote string to be used safely within the Bourne shell.
207
207
208 Example:
208 Example:
209− escapeShellArg "so([<>])me"
209+ escapeShellArg "esc'ape\nme"
210− => "so\\(\\[\\<\\>\\]\\)me"
210+ => "'esc'\\''ape\nme'"
211 */
211 */
212− escapeShellArg = arg:
212+ escapeShellArg = arg: "'${replaceStrings ["'"] ["'\\''"] (toString arg)}'";
213− lib.escape (stringToCharacters "\\ ';$`()|<>\t*[]") (toString arg);
214
213
215− /* Escape all arguments to be passed to the Bourne shell.
214+ /* Quote all arguments to be safely passed to the Bourne shell.
216
215
217 Example:
216 Example:
218− escapeShellArgs ["one" "two three"]
217+ escapeShellArgs ["one" "two three" "four'five"]
219− => "one two\\ three"
218+ => "'one' 'two three' 'four'\\''five'"
220 */
219 */
221 escapeShellArgs = concatMapStringsSep " " escapeShellArg;
220 escapeShellArgs = concatMapStringsSep " " escapeShellArg;
222
221