0

Currently I am having extreme difficulties when putting the output of a command into a variable. I just have no idea what I am doing wrong.

#! /bin/bash
on = $(nmcli networking connectivity)
echo "$on"
if [ "$on" = "full" ]
then
    nmcli networking off
else
    nmcli networking on
fi

edit: when I run this file I get nothing from the echo so I am assuming it is something going wrong with the outputing of the on variable.

  • Possible duplicate of [Command not found error in Bash variable assignment](https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment) – codeforester Jul 30 '17 at 23:22

2 Answers2

0

You will need two equals signs I.e. == for an exact conditional match

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
0
on=$(nmcli networking connectivity)

When creating and initializing variables, you don't have to put spaces before and after the "equal" sign.

if [ "$on" = "full" ]

The equal comparison on strings must be written with double equals: ==

Also be aware that your output can depend on system language! Check this for cross-language support: HERE

FonzTech
  • 408
  • 1
  • 5
  • 13
  • To iterate the reason why it works is because I used spaces between the variable and the $(command) believing that it would be like any other language, apparantly I was wrong and I should not leave spaces. – alphaomega325 Jul 30 '17 at 22:25
  • @FonzTech, you are wrong about the equality testing operator. [`=` is POSIX operator](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html), unlike `==`. And in `bash`, `==` works identical to `=`. On the other hand `=`/`==` behaves differently in `[`/`test` and `[[`. In former case matches only identical strings, and in later case matches with glob pattern. – randomir Jul 30 '17 at 23:26