LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Write output or command to variable (shell) (https://www.linuxquestions.org/questions/programming-9/write-output-or-command-to-variable-shell-534120/)

kernel_geek 03-03-2007 02:01 AM

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.

fukawi2 03-03-2007 03:53 AM

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 "

kernel_geek 03-03-2007 04:04 AM

Yup that worked thanks.

bigearsbilly 03-03-2007 09:16 AM

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


PTrenholme 03-03-2007 09:22 AM

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.

kernel_geek 03-03-2007 03:46 PM

its ment to read output "of"

syg00 03-03-2007 03:58 PM

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.

anguyendapooh 03-11-2007 11:24 PM

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

spaesani 03-12-2007 04:37 AM

or lots of vars:


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



no fan mail please
:))

spaesani 03-12-2007 04:45 AM

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


All times are GMT -5. The time now is 04:57 AM.