Forgive my being a noob. Essentially I'm asking if foo=ls
, when called, would input "ls" or if it would have run ls and then, when called, it would have already run ls, and just use that output. I'm assuming it is the former, in which case, how could you save the output of ls as a variable?
Asked
Active
Viewed 23 times
0
-
This question can teach you a little about ways of doing that http://stackoverflow.com/questions/22709371/bash-backticks-vs-braces – Michael Jan 28 '16 at 03:08
3 Answers
1
foo=$(ls)
You can use $(your command)
to get the output of your command.
In fact, you can't use foo=ls
to make foo
an alias of ls
. You have to write alias "foo"="ls"
so that you can use foo
as ls
.

YCFlame
- 1,251
- 1
- 15
- 24
0
var=ls
is a variable that, when expanded, is ls
. Thus after expansion, bash will execute the command ... if it's the first token in the line. The following are two very different things.
var=ls
$var #very different from the below
echo $var

Andrew Falanga
- 2,274
- 4
- 26
- 51
-1
foo=ls
does not run ls, it just assigns string 'ls' to variable foo.
foo=`ls`
runs ls and capture its output (stdout to be precise) to variable foo.

Jokester
- 5,501
- 3
- 31
- 39