TryItOnline/tryitonline

sed interpreter seems to ignore -r argument

Closed this issue · 3 comments

GNU sed's -r argument turns on Extended Regular Expressions, which let you use e.g. + instead of \+ and ( instead of \(. TryItOnline's sed interpreter, however, seems to ignore this flag when it's added to the "Arguments" section.

For example, this works in bash:

$ echo 'foobar' | sed -r 's/(o)/\1\1/g'
foooobar

But in TryItOnline's sed intepreter with the -r argument it yields the following error:

sed: file .code.tio line 1: invalid reference \1 on `s' command's RHS

You get the same error if you leave off the -r flag in bash.

Poking around a little, it seems that sed cares about order of arguments, and the -f argument must come after the -r argument. In that case, changing the last line in this file from this:

{ [[ -s .input.tio ]] && cat .input.tio || echo; } | sed "${TIO_OPTIONS[@]}" -f .code.tio "$@"

...to this:

{ [[ -s .input.tio ]] && cat .input.tio || echo; } | sed "${TIO_OPTIONS[@]}" "$@" -f .code.tio

...might do the trick.

TIO always adds command-line arguments at the end of the command. I'm not sure how useful that is for sed, but it's what all of the wrappers do.

As you figured out yourself, the sed command that is actually executed is

sed "${TIO_OPTIONS[@]}" -f .code.tio "$@"

That means you can pass -r as a command-line option to achieve the intended effect.

Oh. Well. Now I feel dumb. The distinction between "Options" and "Arguments" was, apparently, beyond me. Thanks.

FWIW, it might be nice if the "Debug" panel also showed the command that was run.