oils-for-unix / oils

Oils is our upgrade path from bash to a better language and runtime. It's also for Python and JavaScript users who avoid shell!
http://www.oilshell.org/
Other
2.85k stars 158 forks source link

Consider improving the handling of traling space after a line continuation #1088

Open hexagonrecursion opened 2 years ago

hexagonrecursion commented 2 years ago

Sometimes we use line continuation:

printf '%s\n' hello \
world

VVV

echo osh:; osh training.sh; echo; echo oil:; oil training.sh 
osh:
hello
world

oil:
hello
world

Sometimes we accidentally leave a trailing space:

printf '%s\n' hello \ 
world

VVV

echo osh:; osh training.sh; echo; echo oil:; oil training.sh 
osh:
hello

  world
  ^~~~~
training.sh:2: 'world' not found

oil:
hello

  world
  ^~~~~
training.sh:2: 'world' not found
training.sh:2: errexit PID 2588: Command failed with status 127

In a more complex example this can be difficult to troubleshoot because the space is invisible.

I think a better behavior for a programming language is to treat <backslash><whitespace>*<LF> as line continuation. It puzzles me why so many languages copy the misfeature of <backslash><LF>. I understand that osh probably has to copy bash, but does oil?

Here are some possible ways of improving the situation:

andychu commented 2 years ago

Yes I think this is a fair point -- I know people try to avoid \ because of this issue.

Oil already warns for something similar:

$ oil -c 'echo \z'
  echo \z
       ^~
[ -c flag ]:1: Invalid char escape (parse_backslash)

So what I think we should do is disallow trailing \ on any word. I think that will be pretty easy to implement. You can always use trailing ' ' which is clearer (or you can use OSH).

Thanks for the feedback!