help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Escape Character and NewLine


From: Fernando Basso
Subject: Re: [Help-bash] Escape Character and NewLine
Date: Mon, 16 Sep 2019 17:09:01 -0300

On Mon, Sep 16, 2019 at 4:40 PM Greg Wooledge <address@hidden> wrote:

> On Mon, Sep 16, 2019 at 04:16:51PM -0300, Fernando Basso wrote:
> > I was reading this section about the Escape Character:
> >
> >
> https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Escape-Character
> >
> > I then tried two examples to test my (mis)understanding:
> >
> > Example 1:
> > ----
> > printf '%s\n' foo\
> >     bar
> > ----
> >
> > And running example 1 produces what I had expected:
> >
> > ----
> > bash dev.sh
> > foo
> > bar
> > ----
>
> At this point, the question is WHY you expected that result, because
> there are a couple different possible trains of thought you could be on.
>
> What actually happens here is the backslash-newline pair is removed,
> and you are essentially running this command:
>
> printf '%s\n' foo    bar
>
> Since printf has a format specifier with only one placeholder in it, but
> it receives two arguments after the format spec, it implicitly loops,
> and applies the format spec to each of the following arguments in turn.
>
> printf '%s\n' foo
> printf '%s\n' bar
>
> OK. That is more or less what I had in mind (that is why I mostly asked
why example 2 didn't work).

"it implicitly loops" is eye-opening, though!


> > However, if I try it in an assignment operation:
> >
> > Example 2:
> > ----
> > str=foo\
> >     bar
> > ----
> >
> > The output is:
> > ----
> > bash dev.sh
> > dev.sh: line 5: bar: command not found
> > ----
> >
> > In example 2, isn't the backslash causing the \newline to be treated as a
> > line
> > continuation and being effectively ignored?
> >
> > What is going on here?
>
> Again, the backslash+newline pair is removed, and you are basically
>> running this command:
>>
>> str=foo    bar
>>
>
Now it looks obvious, but I really didn't even get close to making that
association!


> This attempts to run the command "bar" with the environment variable
> str=foo in its execution environment.
>
> Since you don't have a command named bar on your system, you get that
> result.
>
> You can double-check me by running commands like:
>
> wooledg:~$ str=foo\
> >     printenv str
> foo
>
> In this case, the backslash+newline is removed, so I'm running
>
> str=foo    printenv str
>
> which runs the command "printenv str" with the environment variable
> str=foo in its execution environment.  Thus, it prints "foo".
>

After your explanation, I was able to create another example (which is just
a dumber
variatiation of your printenv one):

$ str=foo\
>  env | grep foo
str=foo

And of course, I need at least one space after the PS2 prompt (since I
don't have
a space before the \ character).

Great explanation. Much appreciated. Thanks a lot!


reply via email to

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