Shell script: Using own IP adresss as variable

Data Mastery :

I have to write a shell script which initializes docker swarm.

I need to pass the own IP address as argument:

 sudo docker swarm init --advertise-addr 167.172.176.134

How can I do this dynamically? I know the ifconfig command, which allows me to see the IP, but I can not pass the whole thing as variable. It should look like this

ip = ifconfig["IP"]
sudo docker swarm init --advertise-addr $ip
prophet :

The following command will return your public ip address

curl ifconfig.me

Please try the following

ip=$(curl ifconfig.me)
sudo docker swarm init --advertise-addr $ip

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=173873&siteId=1