3

OS:Yocto

I want to assign the shell output to a variable,

but get the error "test.sh: line 3: out: command not found".

How to do that ...?

this is my code:

#!/bin/bash

out = "$(ls /dev/ | grep "tty" | wc -l)"
echo"$out"

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

Community
  • 1
  • 1
KBKai
  • 353
  • 1
  • 4
  • 18

3 Answers3

4

Whitespace matters.

#!/bin/bash

out="$(ls /dev/ | grep "tty" | wc -l)"
echo "$out"
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

when you assign value to variable don't keep Whitespace before and after "=" that makes error in bash

#!/bin/bash

out="$(ls /dev/ | grep "tty" | wc -l)"
echo"$out"
DD Dev
  • 859
  • 1
  • 6
  • 11
0

Try strip spaces around =, i.e. out="$(ls /dev/ | grep "tty" | wc -l)"

Jokester
  • 5,501
  • 3
  • 31
  • 39