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: Greg Wooledge
Subject: Re: [Help-bash] Escape Character and NewLine
Date: Mon, 16 Sep 2019 15:40:56 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

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

> 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

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".



reply via email to

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