"echo $foo | bar" paradigm
"echo $foo | bar" paradigm
am 19.09.2007 14:40:12 von flub
Hi
I find myself often doing things like:
$(echo $variable | tr ' ' ',')
or similar. But I was wondering if there is a more stylish way of
using a variable as stdin for a command. I know that in bash you can
use
$(tr ' ' ',' <<<$variable)
but that is still only a bashism.
Anyone know of a normal POSIX sh way of doing this cleaner then the
"echo $foo |" thing?
Regards
Floris
Re: "echo $foo | bar" paradigm
am 19.09.2007 15:21:56 von Janis Papanagnou
On 19 Sep., 14:40, Floris Bruynooghe
wrote:
> Hi
>
> I find myself often doing things like:
>
> $(echo $variable | tr ' ' ',')
>
> or similar. But I was wondering if there is a more stylish way of
> using a variable as stdin for a command. I know that in bash you can
> use
>
> $(tr ' ' ',' <<<$variable)
>
> but that is still only a bashism.
No, it's also in ksh93 - I think even _invented_ there.
> Anyone know of a normal POSIX sh way of doing this cleaner then the
> "echo $foo |" thing?
The above "here strings" have been introduced as a clear way for a
single line of data. For longer data (or to stay within the POSIX
subset of features) use "here documents".
For simple replacements like in your above construct there are also
other (non-POSIX) features in newer shells, like the replacement
${var// /,}
Janis
> Regards
> Floris
Re: "echo $foo | bar" paradigm
am 19.09.2007 17:19:16 von Stephane CHAZELAS
2007-09-19, 06:21(-07), Janis:
[...]
>> $(tr ' ' ',' <<<$variable)
>>
>> but that is still only a bashism.
>
> No, it's also in ksh93 - I think even _invented_ there.
[...]
Nope, it comes for rc. zsh was the first to bring it to the
Bourne-like world, then ksh93 followed and then bash.
The ksh93 ChangeLog (RELEASE file) actually wrongly ackowledges
zsh for that (IIRC).
Of course, it's also in all the rc derivatives.
And of course, it is not POSIX.
--
Stéphane
Re: "echo $foo | bar" paradigm
am 19.09.2007 17:21:33 von Stephane CHAZELAS
2007-09-19, 05:40(-07), Floris Bruynooghe:
[...]
> I find myself often doing things like:
>
> $(echo $variable | tr ' ' ',')
>
> or similar. But I was wondering if there is a more stylish way of
> using a variable as stdin for a command. I know that in bash you can
> use
>
> $(tr ' ' ',' <<<$variable)
>
> but that is still only a bashism.
nope, as seen in other posts.
>
> Anyone know of a normal POSIX sh way of doing this cleaner then the
> "echo $foo |" thing?
[...]
printf '%s\n' "$foo" | thing
is a good start. As "echo $foo" is definitely wrong.
p() { printf '%s\n' "$@"; }
p "$foo" | thing
is another idea.
--
Stéphane
Re: "echo $foo | bar" paradigm
am 19.09.2007 19:52:27 von Michal Nazarewicz
> 2007-09-19, 05:40(-07), Floris Bruynooghe:
> [...]
>> I find myself often doing things like:
>>
>> $(echo $variable | tr ' ' ',')
>>
>> or similar. But I was wondering if there is a more stylish way of
>> using a variable as stdin for a command.
[...]
>> Anyone know of a normal POSIX sh way of doing this cleaner then the
>> "echo $foo |" thing?
Stephane CHAZELAS writes:
> printf '%s\n' "$foo" | thing
I'm always using $(printf %s "$foo" | thing) since if we are using
command substitution the final new line will be removed anyway.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--
Re: "echo $foo | bar" paradigm
am 19.09.2007 19:55:54 von Stephane CHAZELAS
2007-09-19, 19:52(+02), Michal Nazarewicz:
[...]
>> printf '%s\n' "$foo" | thing
>
> I'm always using $(printf %s "$foo" | thing) since if we are using
> command substitution the final new line will be removed anyway.
But here you're piping into some command. And most commands
expect a text input. And text has to be newline terminated.
Some commands will ignore what's after the last \n. And for the
great majority of text utilities, as per POSIX, the behavior is
unspecified.
So, I would add the extra \n to be on the safe side.
--
Stéphane