Skip to content
Advertisement

Shell script: Using own IP adresss as variable

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement