Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use return value of a shell command as a value in shell script? [duplicate]

Tags:

linux

shell

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.

like image 836
erogol Avatar asked Oct 21 '12 23:10

erogol


2 Answers

#!/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.

like image 150
dearN Avatar answered Oct 03 '22 07:10

dearN


variableA=$(ls)
echo "$variableA"
like image 29
Jonathan Leffler Avatar answered Oct 03 '22 08:10

Jonathan Leffler