LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-03-2007, 02:01 AM   #1
kernel_geek
Member
 
Registered: Jan 2007
Location: UK
Distribution: Ubuntu/Arch
Posts: 161

Rep: Reputation: 30
Write output or command to variable (shell)


Hi i want to write an output of a command to a variable, How do I do this is shell script ?

Many Thanks.
 
Old 03-03-2007, 03:53 AM   #2
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
I presume you mean in a bash command...? Something like this...?

Code:
fukawi2@fukawi-desktop:~$ CMDOUT=`uname -r`
fukawi2@fukawi-desktop:~$ echo $CMDOUT
2.6.17-10-generic
Note the quotes after the = sign are back ticks:
` not ' or "

Last edited by fukawi2; 03-03-2007 at 03:55 AM.
 
Old 03-03-2007, 04:04 AM   #3
kernel_geek
Member
 
Registered: Jan 2007
Location: UK
Distribution: Ubuntu/Arch
Posts: 161

Original Poster
Rep: Reputation: 30
Yup that worked thanks.
 
Old 03-03-2007, 09:16 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
or use set:
Code:
$ date
Sat Mar  3 15:15:35 GMT 2007
$ set -- $(date)
$ echo $6 $5 $4 $3 $2 $1
2007 GMT 15:15:46 3 Mar Sat
 
Old 03-03-2007, 09:22 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
If you were asking about doing it within a bash script, run info bash and look at the descriptions of the $(expression) and ${expression} constructions.
 
Old 03-03-2007, 03:46 PM   #6
kernel_geek
Member
 
Registered: Jan 2007
Location: UK
Distribution: Ubuntu/Arch
Posts: 161

Original Poster
Rep: Reputation: 30
its ment to read output "of"
 
Old 03-03-2007, 03:58 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,278

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
Quote:
Originally Posted by bigearsbilly
Sat Mar 3 15:15:35 GMT 2007
$ set -- $(date)
$ echo $6 $5 $4 $3 $2 $1
2007 GMT 15:15:46 3 Mar Sat
Cute.
You learn something all the time.
 
Old 03-11-2007, 11:24 PM   #8
anguyendapooh
LQ Newbie
 
Registered: Feb 2007
Posts: 9

Rep: Reputation: 0
Posted by: Kernel geek
Hi i want to write an output of a command to a variable, How do I do this is shell script ?

Hey, let's try this
who.out=`who | wc -l`
echo $who.out # who.out is your output variable
(or)
LS=`ls -l` # place the value of ls-l into variable LS
echo $LS # and LS is your output of variable

Last edited by anguyendapooh; 03-11-2007 at 11:29 PM.
 
Old 03-12-2007, 04:37 AM   #9
spaesani
LQ Newbie
 
Registered: Mar 2007
Posts: 13

Rep: Reputation: 0
or lots of vars:


for var in $(ls /); do
echo $var
done



no fan mail please
)
 
Old 03-12-2007, 04:45 AM   #10
spaesani
LQ Newbie
 
Registered: Mar 2007
Posts: 13

Rep: Reputation: 0
i can't help myself...

create a little script:

#!/bin/sh
lsmsg=hello
for var in $(ls /); do
echo $var-$lsmsg
done

make an alias:
alias ls=scriptname

now, type ls
nice?
alas the many ways to personalize your pc.

cheers
sp

ps:
want to get rid of the new and improved ls command?
unalais ls
 
  


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
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM
series-Write a single shell command to display the following output ricki Programming 2 07-31-2005 03:53 PM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM


All times are GMT -5. The time now is 03:42 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration