From 41d4d70b985f665c8ecc66b83aa10209c8dfbbfd Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 25 Dec 2023 17:35:44 +0900 Subject: [PATCH] Fix shell escaping for fish Fix #3224 --- src/terminal_unix.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/terminal_unix.go b/src/terminal_unix.go index 1ce7854..c7fa7f1 100644 --- a/src/terminal_unix.go +++ b/src/terminal_unix.go @@ -11,6 +11,20 @@ import ( "golang.org/x/sys/unix" ) +var escaper *strings.Replacer + +func init() { + tokens := strings.Split(os.Getenv("SHELL"), "/") + if tokens[len(tokens)-1] == "fish" { + // https://fishshell.com/docs/current/language.html#quotes + // > The only meaningful escape sequences in single quotes are \', which + // > escapes a single quote and \\, which escapes the backslash symbol. + escaper = strings.NewReplacer("\\", "\\\\", "'", "\\'") + } else { + escaper = strings.NewReplacer("'", "'\\''") + } +} + func notifyOnResize(resizeChan chan<- os.Signal) { signal.Notify(resizeChan, syscall.SIGWINCH) } @@ -29,5 +43,5 @@ func notifyOnCont(resizeChan chan<- os.Signal) { } func quoteEntry(entry string) string { - return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'" + return "'" + escaper.Replace(entry) + "'" }