You can reduce the number of commands in it.
First of all, it's easy to combine these two commands:
awk '{print $5}' | sed -n '2p'
This is exactly the same:
awk 'NR == 2 {print $5}'
It's also easy to combine the two other sed
commands:
sed 's/tcp:\/\///' | sed 's/\:2376//'
Like this:
sed -e 's/tcp:\/\///' -e 's/\:2376//'
By doing so you reduce the number of processes to run,
making the pipeline of commands more efficient.
These last sed
commands can be written a bit simpler by using a different separator character, and avoiding unnecessary escapes:
sed -e 's?tcp://??' -e 's/:2376//'
And the last term might be better more generalized:
sed -e 's?tcp://??' -e 's/:.*//'
Putting this all together:
export dockerip="$(docker-machine ls | awk 'NR == 2 {print $5}' | sed -e 's?tcp://??' -e 's/:.*//')"
You could actually still go further,
and completely eliminate sed
by moving those operations into awk
.
But this is already much more efficient than the original,
simple, and probably good enough.