help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] read that understands quoting


From: Stephane Chazelas
Subject: Re: [Help-bash] read that understands quoting
Date: Mon, 12 Mar 2012 13:11:34 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2012-03-10 14:26:28 -0600, Bill Gradwohl:
> I've been experimenting with read and can't figure out a way to have it act
> like what happens when quotes are used on a command line.
> 
> If I have the following:
> 
> someScript     'this is one field'        thisisanotherfield        'This
> is the last field'
> 
> I get 3 passed parameters. If I read a line from a file with the identical
> parameters, I get 10 fields as the quoting is ignored.
> 
> Is there a way to get the read to go along with the quoting?
> Is there some other mechanism that can do it?
[...]

If switching to zsh is an option:

~$ IFS= read -r string
asd 'qweq asd' qweqwe
~$ print -l ${(z)string}
asd
'qweq asd'
qweqwe
~$ print -l ${(Q)${(z)string}}
asd
qweq asd
qweqwe

So:

IFS= read -r string
array=(${(Q)${(z)string}})

Or to preserve empty elements ('' ""):

array=("${(Q@)${(z)string}}")

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]