LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script: put the output of a command into a variable. (https://www.linuxquestions.org/questions/programming-9/script-put-the-output-of-a-command-into-a-variable-596768/)

poincare999 11-02-2007 11:50 PM

Script: put the output of a command into a variable.
 
I want the output of command1 goes into variable A .
For instance, if command1 = echo -n "148" , I want put 148 into A .

I have done some tries, but these doesn't work:

Code:

echo -n "148" > A
echo -n "148" > A=
echo -n "148" | A
echo -n "148" | A=
echo -n "148" |  read A
....etc

Tks

ocicat 11-03-2007 12:22 AM

Quote:

Originally Posted by poincare999 (Post 2946474)
I want the output of command1 goes into variable A .

Code:

#!/bin/sh
a=$(echo -n '148')
echo $a


poincare999 11-03-2007 12:43 AM

Tks a lot, it works.

doc.nice 11-04-2007 07:01 AM

Next time, you may try searching the net or this forum, this question is very common.
When you provide more information, such as the script LANGUAGE you are using, you will get better results.

another way that would work in bash/ash/tcsh/... is
varname=`command`

(where the ` are french accents grâve, not apostrophies.)

PAix 11-04-2007 11:53 AM

The ` are also known as backticks. in bash the construct $( ) can be used in place of backticks, with the added advantage that the newer construct can be nested.

If you fail to learn something new here every day, check, because you may have died and just not realised it.


PAix


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