[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bash: proposal for >>> operator
From: |
Richard Neill |
Subject: |
Bash: proposal for >>> operator |
Date: |
Wed, 23 Jul 2008 02:52:13 +0100 |
User-agent: |
Thunderbird 2.0.0.14 (X11/20080629) |
Dear All,
Might I propose bash should add another operator, >>> for "redirection
into a variable". This would be by analogy with the <<< operator.
For example, we can currently use <<< to save an "echo", by doing this:
TEXT="Hello World"
grep -o 'hello' <<<"$TEXT"
instead of
TEXT="Hello World"
echo "$TEXT" | grep -o 'hello'
I am therefore proposing that the following syntax would be useful:
echo "Hello World" >>> TEXT
creates a variable named TEXT, whose contents is the string "Hello".
Why is this useful?
1. Read has a nasty habit of creating subshells. For example,
echo Hello | read TEXT
doesn't do what we expect. TEXT is empty!
2. The $() or `` constructs are great, excepting that they also create
subshells. This messes up things like PIPESTATUS.
For example:
echo hello | cat | cat | cat
#hello
echo ${PIPESTATUS[@]}
#0 0 0 0
TEXT=$(echo hello | cat | cat | cat )
echo ${PIPESTATUS[@]}
#0
Here we've captured the output we wanted, but lost the pipestatus.
3. The $() construct doesn't let you capture both stderr and stdout
into different variables.
I know I could do it all with tempfiles, but that somewhat misses the point.
Incidentally, if this is useful, it would be nice to support the
rather prettier counterpart to the <<< operator, and permit this usage:
"$TEXT" >>> grep -o 'hello'
What do you think?
Regards,
Richard