[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Word splitting
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Word splitting |
Date: |
Thu, 3 Oct 2019 09:04:35 -0400 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
On Thu, Oct 03, 2019 at 01:48:08PM +0200, Paul Wagner wrote:
> Example:
>
> $ s='aaa "bbb ccc" ddd'
> $ for p in xxx $s zzz; do echo $p; done
No. Use an array.
Also, learn how to quote correctly. echo $p is a huge danger sign.
https://mywiki.wooledge.org/BashPitfalls#pf14
https://mywiki.wooledge.org/Quotes
> My practical solution would be arrays, but that feels not very elegant,
Adjust your brain until it feels elegant, because it's the correct
solution.
s=(aaa "bbb ccc" ddd)
for p in xxx "${s[@]}" zzz; do echo "$p"; done
# ... or ...
printf '%s\n' xxx "${s[@]}" zzz
If you can't live with this, then stop using bash. Use a language
that better matches your needs. Have you considered Tcl?
% set s {aaa {bbb ccc} ddd}
aaa {bbb ccc} ddd
% puts [join [concat xxx $s zzz] \n]
xxx
aaa
bbb ccc
ddd
zzz
If you don't like that, there's Python, Ruby, Perl, ....