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.

1
  • I think the bit about Arrays is the key differentiating factor from most possible duplicates. Interesting question, don't close it. Commented Oct 22, 2012 at 6:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can I get a array of the values
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 And let us know how it goes!
3
variableA=$(ls)
echo "$variableA"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.