Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: line continuation with sed
- X-seq: zsh-users 28212
- From: Aaron Schrab <aaron@xxxxxxxxxx>
- To: Ray Andrews <rayandrews@xxxxxxxxxxx>
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: line continuation with sed
- Date: Fri, 14 Oct 2022 22:34:30 -0400
- Archived-at: <https://zsh.org/users/28212>
- Feedback-id: i1fd14616:Fastmail
- In-reply-to: <9184bbff-9e68-7b02-2595-3474b016dfff@eastlink.ca>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: Ray Andrews <rayandrews@xxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- References: <9184bbff-9e68-7b02-2595-3474b016dfff@eastlink.ca>
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:
args=(
-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 ]
then
args+=(-re 's/new/old/')
fi
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