Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script: Using own IP adresss as variable

Tags:

linux

bash

shell

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
like image 437
Data Mastery Avatar asked Feb 23 '26 01:02

Data Mastery


1 Answers

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
like image 55
Jeff Huang Avatar answered Feb 24 '26 15:02

Jeff Huang