[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature Request: Custom delimeter for single quotes

From: Patrick Blesi
Subject: Re: Feature Request: Custom delimeter for single quotes
Date: Fri, 1 Nov 2019 14:57:33 -0500

The actual use case is taking a command from a Ruby script:


`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=$(cat <<'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 <address@hidden> 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=""
> nl='
> '
> echo "Enter text, end with ^D:"
> while IFS= read -r line; do
>      input="$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=$(< 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=$(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'
> >
> > Everything within the MAGIC_WORD declarations would not have command
> > substitution, variable expansion, etc, but would be treated as if it were
> > wrapped in single quotes with the exception that single quotes between
> the
> > MAGIC_WORDs need not be escaped.
> >
> > Pardon my naïveté, does any such feature exist or are there good ways to
> > accomplish this? If not, is this something that could feasibly be
> > implemented? Would it be desirable?
> >
> > Thanks,
> >
> > Patrick
> >
> --
> Ilkka Virta / address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]