LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2009, 04:36 AM   #1
tianlijian
LQ Newbie
 
Registered: Mar 2007
Posts: 27

Rep: Reputation: 15
echo oneone | read one


Code:
echo oneone | read one
echo $one
Why the `echo $one' command print nothing?
 
Old 04-27-2009, 05:48 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

As stated in the Advanced Bash-Scripting Guide (chapter 11, below Example 11-6):
Quote:
Piping output to a read, using echo to set variables will fail.
There is an alternative given by the guide:
Code:
#!/bin/bash
#  badread.sh:
#  Attempting to use 'echo and 'read'
#+ to assign variables non-interactively.

a=aaa
b=bbb
c=ccc

echo "one two three" | read a b c
# Try to reassign a, b, and c.

echo
echo "a = $a"  # a = aaa
echo "b = $b"  # b = bbb
echo "c = $c"  # c = ccc
# Reassignment failed.

# ------------------------------

# Try the following alternative.

var=`echo "one two three"`
set -- $var
a=$1; b=$2; c=$3

echo "-------"
echo "a = $a"  # a = one
echo "b = $b"  # b = two
echo "c = $c"  # c = three 
# Reassignment succeeded.
Hope this helps.
 
Old 04-27-2009, 06:38 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 727

Rep: Reputation: 74
Hi.

There are other techniques as well; some will allow you to use read. Here are a few:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate techniques for using variables from bash read.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
# set -o nounset

echo
echo " Results from pipe (expect to fail):"
echo hi |
read t1
echo " t1 is :$t1:"

echo
echo " Results from process substitution:"
read t1 < <( echo lo )
echo " t1 is :$t1:"

echo
echo " Results from grouping with curly braces (create scope):"
echo Hi |
{ read t1
  echo " t1 (inside  braces) is :$t1:"
}
echo " t1 (outside braces) is :$t1:"

exit 0
Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0

 Results from pipe (expect to fail):
 t1 is ::

 Results from process substitution:
 t1 is :lo:

 Results from grouping with curly braces (create scope):
 t1 (inside  braces) is :Hi:
 t1 (outside braces) is :lo:
See abs as noted above and man bash ... cheers, makyo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo '$1' kaviazzz Linux - Software 4 08-17-2007 03:10 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 09:25 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 03:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 03:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 02:13 PM


All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration