LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-24-2005, 03:08 AM   #1
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Rep: Reputation: 15
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
 
Old 08-24-2005, 03:41 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,
Code:
var=`echo "Hello World"`
won't work?
regards
slackie1000
 
Old 08-24-2005, 03:48 AM   #3
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by slackie1000
hi there,
Code:
var=`echo "Hello World"`
won't work?
regards
slackie1000
works for me....
 
Old 08-24-2005, 04:31 AM   #4
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
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
 
Old 08-24-2005, 04:34 AM   #5
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
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?
 
Old 08-24-2005, 05:04 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.
 
Old 08-24-2005, 05:09 AM   #7
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
thanks theYinYeti that second method is handy

is it possible to append the stderr of a command to a variable?
 
Old 08-24-2005, 05:33 AM   #8
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.
 
Old 08-24-2005, 05:36 AM   #9
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
ah of course, cheers
 
Old 08-24-2005, 05:41 AM   #10
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
*slackie1000 hits his head in the wall and buys an english dictionary*
sorry mate...
my mistake..
regards,
slackie1000
 
Old 08-24-2005, 05:42 AM   #11
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
lol, no worries
 
  


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
pipe output to append to a text file davee Linux - Newbie 5 03-22-2016 07:44 PM
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 11:13 AM
series-Write a single shell command to display the following output ricki Programming 2 07-31-2005 03:53 PM
Saving the output of a file as a variable mrobertson Programming 5 07-08-2005 12:43 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:52 AM.

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
Open Source Consulting | Domain Registration