0

I am using wc -l < songs.txt to find out how many lines are in songs.txt

Then I want to store that as a variable. I've tried these:

OUT = $(wc -l < songs.txt)
OUT = "$(wc -l < songs.txt)"

I tried a lot from How to set a variable to the output from a command in Bash?

But I still get command not found error for OUT

wc -l < songs.txt works on its own

What simple thing am I missing here?

Community
  • 1
  • 1
Maxwell Chandler
  • 626
  • 8
  • 18

2 Answers2

0

it's easy first of all don't use space after and before equal sign

OUT=$(wc -l < songs.txt)

second for call it you can write something like this :

echo $OUT

Good luck

Freeman
  • 9,464
  • 7
  • 35
  • 58
  • You should double-quote variable references, e.g. `echo "$OUT"`. – Gordon Davisson Jan 29 '17 at 20:47
  • @GordonDavisson, thank you , is it a rule ? because it's working without "" too! – Freeman Jan 31 '17 at 09:49
  • 1
    Not a strict rule, and it'll *often* work without double-quotes, but there are enough cases where *not* double-quoting variables [will](http://stackoverflow.com/questions/26616849/how-to-set-shell-variable-to-result-of-evaluation-in-os-x) [cause](http://stackoverflow.com/questions/41793270/grep-on-a-variable-containing-a-string-with-a-tab/41793296) [trouble](http://superuser.com/questions/655926/why-does-checking-whether-a-folder-exists-using-d-on-an-empty-string-return-tru) that it's safest to treat it as a rule. – Gordon Davisson Jan 31 '17 at 16:10
  • good to know, thank you brother , 1++ :D – Freeman Jan 31 '17 at 18:13
0

Spaces should be avoided.

out=$(wc -l < songs.txt)
Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38