summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorzimbatm <zimbatm@zimbatm.com>2016-06-21 14:32:45 +0100
committerGitHub <noreply@github.com>2016-06-21 14:32:45 +0100
commitbc6b93511f21f9943b1c9a9b872717d507ab9070 (patch)
tree0e704bbd391a6beb02075db98cfc4af25f45e619 /lib
parent6536804848f3989977441d92d77aa9230dd07018 (diff)
parentdf475092e92b9dab9642c48f2216d49027a457a1 (diff)
downloadnixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.tar
nixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.tar.gz
nixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.tar.bz2
nixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.tar.lz
nixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.tar.xz
nixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.tar.zst
nixpkgs-bc6b93511f21f9943b1c9a9b872717d507ab9070.zip
Merge pull request #16377 from aszlig/improve-escape-shell-arg
lib: Make escapeShellArg more robust
Diffstat (limited to 'lib')
-rw-r--r--lib/strings.nix15
1 files changed, 7 insertions, 8 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 04376a5f2fb..5e5f7b37866 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -203,20 +203,19 @@ rec {
   */
   escape = list: replaceChars list (map (c: "\\${c}") list);
 
-  /* Escape all characters that have special meaning in the Bourne shell.
+  /* Quote string to be used safely within the Bourne shell.
 
      Example:
-       escapeShellArg "so([<>])me"
-       => "so\\(\\[\\<\\>\\]\\)me"
+       escapeShellArg "esc'ape\nme"
+       => "'esc'\\''ape\nme'"
   */
-  escapeShellArg = arg:
-    lib.escape (stringToCharacters "\\ ';$`()|<>\t*[]") (toString arg);
+  escapeShellArg = arg: "'${replaceStrings ["'"] ["'\\''"] (toString arg)}'";
 
-  /* Escape all arguments to be passed to the Bourne shell.
+  /* Quote all arguments to be safely passed to the Bourne shell.
 
      Example:
-       escapeShellArgs ["one" "two three"]
-       => "one two\\ three"
+       escapeShellArgs ["one" "two three" "four'five"]
+       => "'one' 'two three' 'four'\\''five'"
   */
   escapeShellArgs = concatMapStringsSep " " escapeShellArg;