[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Transform strings with special characters so that the st
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted? |
Date: |
Mon, 30 Mar 2015 15:39:58 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2015-03-30 09:47:16 -0400, Greg Wooledge:
> On Mon, Mar 30, 2015 at 02:02:10PM +0100, Stephane Chazelas wrote:
[...]
> > var=$(cmd && echo .) && var=${var%?} && var=${var%?}
> > (to remove only one newline)
> >
> > but that's obviously a pain.
>
> In #bash we (de facto) agreed on this version:
>
> var=$(cmd; printf x) var=${var%x}
>
> Which is basically the same as yours
That doesn't trim the one trailing newline you usually want to
trim (which was the point of my two var=${var%?}) and that fails
to preserve the exit status of cmd though.
There's a tradeoff in the solution I gave as well in that it
won't preserve the trailing newlines if cmd fails.
I often do:
var=$(cmd && echo .) || exit
var=${var%?}; var=${var%?}
Where that's not a problem.
A complete solution would be:
var=$(cmd; ret=$?; echo .; exit -- "$ret")
ret=$?
var=${var%?}; var=${var%?}
(exit -- "$ret")
Probably better at that point defining a function for that, like
that I gave at https://unix.stackexchange.com/a/120775 where we
discussed that before.
cmdsubst() {
local _ret _var
_var=$1; shift
eval "$_var"'=$("$@"; _ret=$?; echo .; exit -- "$_ret")
_ret=$?
'"$_var=\${$_var%?}; $_var=\${$_var%?}"
return -- "$_ret"
}
And use as:
cmdsubst var cmd args...
(yes, I know "--" is currently not needed for current versions
of bash, that's just me being paranoidly future-proof in case
bash starts allowing negative values or arbitrary strings for
return like rc/es in the future;-))
--
Stephane
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, (continued)
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Eric Blake, 2015/03/25
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Eric Blake, 2015/03/25
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Chet Ramey, 2015/03/26
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Eric Blake, 2015/03/26
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/29
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Greg Wooledge, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Greg Wooledge, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?,
Stephane Chazelas <=
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Greg Wooledge, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/29
Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/25