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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-24-2005, 03:08 AM
|
#1
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Rep:
|
shell scipting: append output of a command to a variable
hi
doing a little bit of shell scripting.....
it's possible to append the output of a command to a file:
eg. echo "hello" >> file
but is there an equivalent to append the output to a variable? (i'm building up a log message which i want to both write to a log file and email)
the only way i currently know how to do this is:
var=${var}`echo "hello"`
thanks for your help
jon
|
|
|
08-24-2005, 03:41 AM
|
#2
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
Code:
var=`echo "Hello World"`
won't work?
regards
slackie1000
|
|
|
08-24-2005, 03:48 AM
|
#3
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
Quote:
Originally posted by slackie1000
hi there,
Code:
var=`echo "Hello World"`
won't work?
regards
slackie1000
|
works for me.... 
|
|
|
08-24-2005, 04:31 AM
|
#4
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
so, if it works... what is your question? 
i thought, you want to have the output of a command assigned to a variable.
regards,
slackie1000
|
|
|
08-24-2005, 04:34 AM
|
#5
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
i want to append the output to a variable, ie. keeping its existing value, and appending the new data
var=${var}`echo "hello"` will do it, but i was wondering whether there's a cleaner way using the >> operator maybe?
|
|
|
08-24-2005, 05:04 AM
|
#6
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
I know two methods:
1/ You don't need a line break:
Code:
var="${var}$(/path/to/command params)"
2/ You need a line break:
Code:
var="$(echo "$var"; /path/to/command params)"
Yves.
Last edited by theYinYeti; 08-24-2005 at 05:07 AM.
|
|
|
08-24-2005, 05:09 AM
|
#7
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
thanks theYinYeti that second method is handy
is it possible to append the stderr of a command to a variable?
|
|
|
08-24-2005, 05:33 AM
|
#8
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Of course. Instead of writing:
/path/to/command params
you write:
/path/to/command params 2>&1
if you want both stdout and stderr,
/path/to/command params 2>&1 >/dev/null
if you only want stderr.
Yves.
|
|
|
08-24-2005, 05:36 AM
|
#9
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
ah of course, cheers 
|
|
|
08-24-2005, 05:41 AM
|
#10
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
*slackie1000 hits his head in the wall and buys an english dictionary*
sorry mate...
my mistake..
regards,
slackie1000
|
|
|
08-24-2005, 05:42 AM
|
#11
|
Member
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71
Original Poster
Rep:
|
lol, no worries
|
|
|
All times are GMT -5. The time now is 07:09 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|