3

I have a simple Makefile:

run : 
	time echo foo

Here's the output when I use it:

$ make run
time echo foo
make: time: Command not found
make: *** [Makefile:2: run] Error 127

Why doesn't it work? As I understand, time is keyword in bash (unless you also installed the time program, which I haven't), and Makefile uses sh as the default shell, but I have that simlinked to bash. Here's some other relevant output:

$ type -a time
time is a shell keyword
$ bash --version
GNU bash, version 5.0.7(1)-release (x86_64-pc-linux-gnu)
$ ls -l "$(which sh)"
lrwxrwxrwx 1 root root 4 Apr 30 05:13 /usr/bin/sh -> bash
$ make --version
GNU Make 4.2.1
$ ls -l /bin
lrwxrwxrwx 1 root root 7 May 23 10:18 /bin -> usr/bin
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Apr 30 05:13 /bin/sh -> bash
$ /bin/sh -c 'time true'

real    0m0.000s
user    0m0.000s
sys     0m0.000s

edit: note also that /bin is simlinked to /usr/bin, so the issue is not due to a distinction between /bin/sh and /usr/bin/sh. Also I'm using Arch Linux, with latest pacman -Syu update as of today June 28, 2019.

Also, here is the result of a hexdump of the Makefile:

$ xxd Makefile
00000000: 7275 6e20 3a0a 0974 696d 6520 6563 686f  run :..time echo
00000010: 2066 6f6f 0a                              foo.
  • What is the output of /bin/sh -c 'time true'? – Kamil Maciorowski Jun 29 '19 at 4:10
  • just added that toward the end of the post. – xdavidliu Jun 29 '19 at 10:47
  • the output for make in that case is lrwxrwxrwx 1 xdavidliu xdavidliu 0 Jun 30 09:12 /proc/2476/exe -> /usr/bin/ls – xdavidliu Jun 30 '19 at 13:13
  • Sorry, I forgot one trick shells do. Try ls -l /proc/$$$$/exe; true in the makefile. – Kamil Maciorowski Jun 30 '19 at 16:32
  • okay, the output when I add ; true is lrwxrwxrwx 1 xdavidliu xdavidliu 0 Jun 30 16:31 /proc/11816/exe -> /usr/bin/bash – xdavidliu Jun 30 '19 at 20:32
1

Having sh symlinked to bash does not mean that invoking sh will be equivalent to invoking bash.

This is what Archwiki says:

When Bash, mksh and zsh are invoked with the sh name, 
they automatically become more POSIX compliant.

That means that some features will not be available and you should still choose bash as a shell in your Makefile if you need the features of bash.

Add this as the first line of your Makefile to choose bash as a shell:

SHELL := /bin/bash

This should solve your issue.

More about choosing the shell can be found in the GNU Make documentation

| improve this answer | |
  • 1
    simlinking sh to bash is the default used by archlinux – xdavidliu Oct 24 '19 at 17:19
  • Oh, I didn't realize that. However, according to archwiki the behavior is still different even when symlinked: "When Bash, mksh and zsh are invoked with the sh name, they automatically become more POSIX compliant." wiki.archlinux.org/index.php/Command-line_shell – curusarn Oct 24 '19 at 17:22
  • I'm editing my answer to include that. – curusarn Oct 24 '19 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.