8

Possible Duplicate:
Capturing multiple line output to a bash variable

For example I want to run ls command and take the return list as a value kept in a array in shell script.

Something like

run

#ls
fileA
fileB
fileC

kept this return list in a variable that keeps a array

variable A = ["fileA","fileB","fileC"];

I cannot give the exact notation for code since I do not know how to write shell script. After I learn this, I 'll.

Community
  • 1
  • 1
erogol
  • 13,156
  • 33
  • 101
  • 155
  • I think the bit about Arrays is the key differentiating factor from most possible duplicates. Interesting question, don't close it. – Adam Eberlin Oct 22 '12 at 06:25

2 Answers2

7
#!/bin/bash
variableA=$(ls)
echo $variableA

That should be your shell script assuming that you have bash

Then all you'd need to do is chmod +x shell_script to make it executable.

If you use ls > contents.file the result of ls is saved to a file called contents.file. Remember, > rewrites the entire file while >> appends to the last line.

dearN
  • 1,256
  • 4
  • 19
  • 40
  • can I get a array of the values – erogol Oct 21 '12 at 23:48
  • I am not the best bash guy. So what I do is generally populate files and then read from them again. As for arrays, you'd want to see [this](http://www.linuxquestions.org/questions/linux-general-1/store-multi-line-output-into-an-array-in-a-linux-bash-script-706878/#post3454704) And let us know how it goes! – dearN Oct 21 '12 at 23:52
3
variableA=$(ls)
echo "$variableA"
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278