Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script assign output to a variable fail

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?

like image 349
KBKai Avatar asked Dec 21 '25 18:12

KBKai


2 Answers

Whitespace matters.

#!/bin/bash

out="$(ls /dev/ | grep "tty" | wc -l)"
echo "$out"
like image 125
Ignacio Vazquez-Abrams Avatar answered Dec 23 '25 09:12

Ignacio Vazquez-Abrams


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"
like image 45
DD Dev Avatar answered Dec 23 '25 08:12

DD Dev