github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/shell/escape.go (about) 1 // Copyright (c) 2018, Sylabs, Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please 3 // consult LICENSE.md file distributed with the sources of this project regarding 4 // your rights to use or distribute this software. 5 6 package shell 7 8 import "strings" 9 10 // ArgsQuoted concatenates a slice of string shell args, quoting each item 11 func ArgsQuoted(a []string) (quoted string) { 12 for _, val := range a { 13 quoted = quoted + `"` + Escape(val) + `" ` 14 } 15 quoted = strings.TrimRight(quoted, " ") 16 return 17 } 18 19 // Escape performs escaping of shell quotes, backticks and $ characters 20 func Escape(s string) string { 21 escaped := strings.Replace(s, `\`, `\\`, -1) 22 escaped = strings.Replace(escaped, `"`, `\"`, -1) 23 escaped = strings.Replace(escaped, "`", "\\`", -1) 24 escaped = strings.Replace(escaped, `$`, `\$`, -1) 25 return escaped 26 }