0

before i begin i am aware this may be an issue best opened on git-hub but;

a. it's minor

b. the functionality of the backslash has already been outlined by a previously closed issue but not the white-space (see here)

c. i am unable to open issues on git-hub, being a new user

d. it may help newcomers via google-indexing.

Feel free to create a git-hub issue on my behalf.

Consider the following code;

# Our fish :)
set fishies \
">('>" \
"><=*>" \
">-<==*>" \
">==(--v-*);->" \
"><>" \
"><(((º>" \
">-<===*>" \
">(̠̄:̠̄ >" \
">><((( ◉ >"
    
# Usage ; set fish (getfish) && echo $fish
function getfish
    # Set our fish to a random item from the "fishies" list
    set -l fish $fishies[(random 1 (count $fishies))]

    # Return our fish to the STDOUT
    echo $fish
end

This works fine. However, if we are to introduce a white-space after any backslash ("\__" (indicated by the underscore (_))) the line below it will not be passed to the set fishes command.

Is this a correct behavior and is their any documentation on how the backslash works as I've only stumbled this through tinkering?

To re-iterate, can anyone explain to me why this behavior occurs?

The most i found (i think) in reference to the back-slash is the following code on the git-hub issue aforementioned (i don't fully understand it however);

It (the usage of a backslash to form a new-line) has been special cased in tokenizer.cpp:539 :

    if ((*(tok->buff) == L'\\') &&(*(tok->buff+1) == L'\n'))
    {
        tok->last_pos = tok->buff - tok->orig_buff;
        tok->buff+=2;
        tok->last_type = TOK_END;
        return;
    }

Apologies if this is naive / poorly written i am a new and in-active user. I thought I'd throw my 2 cents in and hopefully help any other newcomers encountering this same issue, it's a great little feature and a lush little tool, it would be a shame to leave this behavior undocumented as it comes in handy when writing long commands/strings.

5
  • 1
    Backslash followed immediately by a literal newline is a special case of Escaping Characters - this behavior is consistent with POSIX Commented Sep 2 at 21:52
  • Thank-you @steeldriver that makes sense. Commented Sep 2 at 21:58
  • ... perhaps I should have added that since fish is intentionally not POSIX-compatible there's nothing to stop you from requesting a different behavior (though you should probably expect some push back) Commented Sep 3 at 0:40
  • @steeldriver but also: "\ (backslash space) escapes the space character. This keeps the shell from splitting arguments on the escaped space." it seems they have already documented this behaviour for another purpose, so I'd thnk it's unlikely they'll change it.
    – muru
    Commented Sep 3 at 3:26
  • That's ok, so long as this is an intended feature and isn't likely to change anytime soon that actually works in pretty good stead for what i want to do. It helps a ton now that i understand it more too. Thank-you both! Commented Sep 3 at 14:42

1 Answer 1

1

A backslash escapes the character that comes after it.

For example \t is tab, \' is a regular single-quote (that doesn't quote anything). When you do \ t or \ ' that escapes the space, not the character after. This can be useful for arguments with spaces like

printf '%s\n' hello\ world

where it will print "hello world". The alternative is to quote it like "hello world" or 'hello world'.

The backslash escapes the character directly after, it's no different for newlines. When you do

\
abc

the line ends in a newline character, and that's what the backslash needs to escape, so it needs to be immediately before the newline or it would escape whatever comes directly after it (like a space).

Changing this in fish would require removing backslash-space. It would make any \ x escape the "x" instead. I can confidently tell you that it won't happen, as it would be a breaking change and remove a useful use of backslash.

It's also not particularly out of the ordinary, posix shells do it the same way.

1
  • Cheers for the explanation @faho ! Commented Sep 3 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .