First: //
is not a comment marker in shell syntax; use #
. Please be very careful about trying to use other language's syntax in the shell, as it's often different. Here is an example where this exact mistake led to an erased server.
Now, to the script:
default_ip=$(hostname -I)
hostname -I
prints all IP addresses for the computer (separated by spaces). $()
runs the contents as a command, and captures that output. The default_ip=
part assigns that output to the default_ip
variable. hostname -I
prints all IP addresses for the computer (separated by spaces), so that's what default_ip
gets set to.
printf Put your local IP
This doesn't work right. Are there supposed to be quotes around the Put your local IP
part? Anyway, printf
prints things, but it's kind of complicated to use right, so I'll just duck that part of the question.
read ip
Reads something from standard input (the terminal, by default), and stores it in the ip
variable.
ip="${ip:-${default_ip}}"
Uses the variable default_ip
as a default value for the ip
variable. The ${thing1:-thing2}
syntax tries to get the value of the variable thing1
, but if it isn't defined as a variable or is defined as the empty string it uses the string thing2
instead. In this case, thing2
is ${default_ip}
, which gets the value of the variable default_ip
.
Basically, this means that if the user just hits return instead of entering an IP address, it uses the output from hostname -I
instead.