Bash script if-statement not working

I'm kind of embarrassed to ask but I can't get this if-statement working.

I have a bash script that automates some stuff at startup depending on the number of monitors I have.
When my external monitor is plugged in, I want to have a different setup than when I'm working on my laptop without external monitor.

displays=$(xrandr | grep -sw 'connected' | wc -l) # the manount of displays
echo "$displays"
if [ $displays==1 ]; then
	echo "1 monitor"
else
	echo "2 monitors"
fi

When I have my external monitor plugged in and $displays is 2, I get the echo "1 monitor"?

Are you using a notebook?

if [ $OUTPUT != $MAIN_DP ]
        then
1 Like

I thought == is a string comparison?

If [ $displays -eq 2 ]

3 Likes

I really dislike bash scripting... for simple automation is fine but I find the syntax to be horrendous to look at, and even worse, memorize because of all the backwards compatibility features it has.

What I like to do is make absolutely sure I got it right by throwing everything that is allowed. That way I can be sure it works:

if [[ "${displays}" -eq 1 ]]
then
	echo "1 monitor"
else
	echo "2 monitors"
fi
1 Like

I also dislike the syntax, it's so weird compared to the other languages I know.

What do you mean by a notebook?

Notebook computer a.k.a. laptop.
I avoid calling it a "laptop" because it should never go on a persons body...

1 Like

Oh, right :sweat_smile:
I was thinking the question was if I was programming it in a notebook, something like jupyter for example.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.