aarshaw aarshaw - 4 months ago 17
Bash Question

Eval in docker-machine: terminal vs shell script

I'm trying to run a simple shell script to automate changing docker-machine environments. The problem is this, when I run the following command directly in the Mac terminal the following is outputted:

eval $(docker-machine env default)
docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * digitalocean Running tcp://***.**.***.***:**** v1.12.0


So basically what you would expect, however when I run the following .sh script:

#!/usr/bin/env bash
eval $(docker-machine env default)


The output is:

./run.sh
docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default digitalocean Running tcp://***.**.***.***:**** v1.12.0


So basically, it is not setting it as active and I cannot access it.

Has anyone run into this issue before and knows how to solve it? Seems really strange to me, have got pretty much everything else running and automated apart from this facet.

Cheers, Aaron

Answer

I think you need to source your shell script

source ./myscript.sh

as the exports in the eval are being returned to the process you started to run the shell in and then being disposed of. These need to go to the parent e.g. login shell

Consider a.sh

#!/bin/bash
eval $(echo 'export a=123')
export b=234

when run in two ways

$ ./a.sh
$ echo $a

$ echo $b


$ source a.sh
$ echo $a
123
$ echo $b
234
$