Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Patrick Blesi Newsgroups: gnu.bash.bug Subject: Re: Feature Request: Custom delimeter for single quotes Date: Fri, 1 Nov 2019 14:57:33 -0500 Lines: 105 Approved: bug-bash@gnu.org Message-ID: References: <13ecc4db-2b5e-95dd-2445-78191b9c01dd@iki.fi> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Trace: usenet.stanford.edu 1572638273 21691 209.51.188.17 (1 Nov 2019 19:57:53 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash@gnu.org To: Ilkka Virta Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ble.si; s=default; t=1572638267; bh=BWbmyTFodNWCbTN0ZMTcQmYrnOfaNRNKZxhdXCzxk0A=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=lgFDQEBZm8yhunYCOo/ugfVg05hXvokmDhevqGxMJSTjZNPBa6h0TiPZQeoaZNh8x /LdYqqQTKJC3o8PMSoj1JrI2GguWPW4YlJnLUyQioJLgg+ZV0G8EuacYumq6e99FE3 sT8Asrx/oi7vYE/vG8wMATzRCL1f4q/+ok4mkmXw= X-Virus-Scanned: Debian amavisd-new at personyms.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ble.si; s=default; t=1572638266; bh=BWbmyTFodNWCbTN0ZMTcQmYrnOfaNRNKZxhdXCzxk0A=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=C6nmT6zojWq27R0gI2gdPhKYjTl4a+p+LP8GC0aEqW6W35dO+XASIYd804/+f9sbJ nUJOqOaQk4wcp80LzsFRESIruDecVBo9PBH4KW6UD4kkzs6kZXXK/PJ6kfsxTzeWff +vx543FouBk94ZpgbuFxaQCewabKn48MHO+J2IDw= X-Gm-Message-State: APjAAAVJ2WPN6UBy5kJUNVzPa68Ju6Vd4loipUrCbXwmVziHwQN3uRUK l1TO4cAAPpRvEzRKmKFjXcFeaxfvQXsYplICysA= X-Google-Smtp-Source: APXvYqxfEyk+i3t5FjzA92o84MESPEIGFyu2ISBFCAF2zou+NVmumVyD5NMR2QDdmo/r21faZoZ0jXDAl+DHAZXQSYc= X-Received: by 2002:aa7:d658:: with SMTP id v24mr14772825edr.301.1572638264758; Fri, 01 Nov 2019 12:57:44 -0700 (PDT) In-Reply-To: <13ecc4db-2b5e-95dd-2445-78191b9c01dd@iki.fi> X-Gmail-Original-Message-ID: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 45.56.123.183 X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <13ecc4db-2b5e-95dd-2445-78191b9c01dd@iki.fi> Xref: csiph.com gnu.bash.bug:15552 The actual use case is taking a command from a Ruby script: https://github.com/braintree/runbook/blob/4a0f0770a8a2a7be135cf13ee435d981b= 5975a06/lib/runbook/helpers/tmux_helper.rb#L23 `tmux send-keys -t #{target} #{_pager_escape_sequence} '#{command}' C-m` The user specifies the command they want to run as a Ruby string and it gets interpolated into the above string and then executed (The backticks in Ruby invoke the command in a subprocess and return the output as a string, #{} is string interpolation). As you can see, if the user-specified command has a single quote, it will break this command unless escaped. I think doing something like this should serve my needs: ` command=3D$(cat <<'MAGIC_WORD' #{command} MAGIC_WORD ) tmux send-keys -t #{target} #{_pager_escape_sequence} "$command" C-m ` So that no single quote escaping is required. The non-valid input for the command would be MAGIC_WORD. Do you know if this command is POSIX compliant/supported by a large number of shells? Is is supported by the bourne shell? On Fri, Nov 1, 2019 at 3:37 AM Ilkka Virta wrote: > On 1.11. 06:54, Patrick Blesi wrote: > > I'm looking for a hybrid between single quotes and a here doc or here > > string. > > > > The main use case is for accepting arbitrary user-specified text. > > Do your users enter the text by directly editing the script? > Would it make more sense to use e.g. 'read' to read the input directly > from the user? > > input=3D"" > nl=3D' > ' > echo "Enter text, end with ^D:" > while IFS=3D read -r line; do > input=3D"$input$line$nl" > done > > printf "You entered:\n---\n%s---\n" "$input" > > > or to just have the text in a separate file (not the script) and read it > from there? > > input=3D$(< inputfile) > > > That way, the text appears in a variable, and you don't need to care > about quotes inside it. > > > (You could also read from stdin with just input=3D$(cat) instead of the > while read loop but that feels a bit odd to me for some reason.) > > > I would > > like to wrap this text in single quotes so as to prevent any variable > > expansion or interpretation of the text of any kind. Additionally, I > would > > like to allow the users to include single quotes in their text without > > requiring that they escape these quotes. > > > > Something akin to the following would alleviate the need to communicate > > that users must escape single quotes, but also provide the same literal > > string behavior of single quotes. > > > > presuming the arbitrarily substituted text is: > > > > echo 'this command is specified by the user' > > > > Then a syntax for this single quote heredoc behavior could be like: > > > > $ sh -c <<^MAGIC_WORD echo 'this command is specified by the user' > > MAGIC_WORD > > > > Everything within the MAGIC_WORD declarations would not have command > > substitution, variable expansion, etc, but would be treated as if it we= re > > wrapped in single quotes with the exception that single quotes between > the > > MAGIC_WORDs need not be escaped. > > > > Pardon my na=C3=AFvet=C3=A9, does any such feature exist or are there g= ood ways to > > accomplish this? If not, is this something that could feasibly be > > implemented? Would it be desirable? > > > > Thanks, > > > > Patrick > > > > > -- > Ilkka Virta / itvirta@iki.fi >