Home > OS >  Running command with make gives different result from running it directly in shell
Running command with make gives different result from running it directly in shell

Time:11-06

I have a Makefile that looks like this:

build-docker:
    DOCKER_BUILDKIT=1 docker build --ssh default=~/.ssh/id_rsa -t my-app .

If I run make build-docker I get the following error:

$ make build-docker
DOCKER_BUILDKIT=1 docker build --ssh default=~/.ssh/id_rsa -t my-app .
could not parse ssh: [default=~/.ssh/id_rsa]: stat ~/.ssh/id_rsa: no such file or directory
make: *** [Makefile:12: build-docker] Error 1

However, if I run the command directly in the shell it runs just fine:

$ DOCKER_BUILDKIT=1 docker build --ssh default=~/.ssh/id_rsa -t my-app .
[ ] Building 65.5s (20/20) FINISHED

Why is this and how do I solve it?

CodePudding user response:

You are running the same command, but in different shells. Your interactive shell is probably bash. But the shell make uses is /bin/sh which is a POSIX standard shell (often).

The special handling of ~ in an argument is a shell feature: it's not embedded in programs like docker or ssh. And, it's not defined in POSIX; it's an additional feature that some shells, like bash.

On my system:

bash$ echo foo=~
foo=/home/me

bash$ /bin/sh

$ echo foo=~
foo=~

To be portable you should use the full pathname or $HOME instead (remember that in a make recipe you have to double the $ to escape it from make: $$HOME).

  • Related