Variable assignments cannot have space around the =
in the shell. Also, you don't want single quotes there, you want either backticks or $()
. The single quotes should only be for your awk command. Your awk is needlessly complicated as well, and you are using command substitution ($()
) when printing, but ipAdresses
is a variable, not a command.
Try something like this:
#!/bin/bash
ipAddresses=$(ifconfig | sed 's/^ *//' | awk -F'[: ]' '/^ *inet addr:/{print $3}')
printf 'Sus direcciones IP son:\n%s\n' "$ipAddresses"
But that is really not portable. You didn't mention your OS, but I am assuming it's a Linux and the output suggests Ubuntu (I don't have addr
after inet
in the output of ifconfig
on my Arch, for example).
If you are running Linux, you could use grep
instead:
ipAddresses=$(ifconfig | grep -oP 'inet addr:\K\S+')
ip
is generally replacing ifconfig
, so try this instead:
ipAddresses=$(ip addr | awk '/inet /{print $2}')
or
ipAddresses=$(ip addr | grep -oP 'inet \K\S+')
Or, to remove the trailing /N
:
ipAddresses=$(ip addr | grep -oP 'inet \K[\d.]+')
And you don't need the variable anyway, you can just:
printf 'Sus direcciones IP son:\n%s\n' "$(ip addr | awk '/inet /{print $2}')"