4

How would I go about saving an output in a vaiable. For example, if I enter:

ls -l

it will show the files. How would I go about saving the output in a variable? Is it possible at all?

Volker Siegel
  • 3,277
  • 2
  • 24
  • 35
user3303592
  • 41
  • 1
  • 2
  • 2
    Welcome to Stack Overflow. Please read the [About] page soon. Your question title mentions C; your tags do not. There's an answer that's appropriate to the tags - but you can and should edit the title if that's correct. If you really want to do it in C, there's a deleted answer that's appropriate; you should retag the question, dropping terminal (and review the bash and shell tags). – Jonathan Leffler Feb 12 '14 at 21:27

2 Answers2

5

You can do the following: var1=$(ls -l)

LeonardBlunderbuss
  • 1,264
  • 1
  • 11
  • 22
2

I know three ways to do:

1) Functions are suitable for such tasks:

func (){
ls -l
}

Invoke it by saying func

2) Also another suitable solution could be eval:

var="ls -l"
eval $var

3) The third one is using variables directly:

var=$(ls -l)
OR
var=`ls -l`

you can get output of third solution in good way:

echo "$var"

and also in nasty way:

echo $var
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
MLSC
  • 5,872
  • 8
  • 55
  • 89