Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: line continuation with sed

At 11:25 -0700 13 Oct 2022, Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
I'm wondering if this is a zsh issue or entirely sed's fault but:

$ var=$( print -l $var | sed \
-re 's/.../' \                                 # this is fine
-re 's/.../'\                                  # this  throws an error which I'd expect
-re 's/.../' \                                  # this throws an error ...
-re 's/.../' )

There have already been a number of replies explaining this, and hints on how to detect this, so I won't address that.

Instead I'll give a method to help avoid the problem. For awhile now, anytime I'm using calling a command with enough options to warrant line continuation I build them up in an array instead. This allows splitting across lines without using a backslash at the end, and *actually* allows comments.

This also allows the arguments to be build up gradually with some being added conditionally.

The below actually works as written including the comments:

      -re 's/abc/xyx/' # End of the alphabet is better
      -re 's/123/456/' # Bigger numbers are better
      # -re 's/foo/bar/' # Disable for now

    if [ -r /some/file ]
      args+=(-re 's/new/old/')

    echo abc123 | sed "${args[@]}"

For zsh you can just use $args to reference it, the way I wrote it above will work in bash as well.

Messages sorted by: Reverse Date, Date, Thread, Author