2

I just solved a problem with my Makefile(s). Make trips over every <<< with the error message

/bin/sh: 1: Syntax error: redirection unexpected

And I would like to know why. (I am using Bash as SHELL)

In my current projects I tried a lot of recipies along the lines of:

target:
    read FOO <<< "XXX"; \
    read BAR <<< "YYY"; \
    read BAZ <<< "ZZZ"; \
    someprog -a param0 -b $$FOO -c param1 -d $${BAR} -e param2 -f $${BAZ} >$@

Trying this will result in an error for every <<< as described at the beginning. My workaround is

target.dep:
    echo "XXX YYY ZZZ" >$@

target: %: %.dep
    read FOO BAR BAZ < $<;\
    someprog -a param0 -b $$FOO -c param1 -d $${BAR} -e param2 -f $${BAZ} >$@

which means I put my stuff into temporary files which I then read with <, which works just fine. When I copy paste my make output to a normal bash prompt, every command works just as expected, even with the <<<. I am fairly certain that my problem is, that using the <<< operator, i.e. here strings, break something. Why is that and is there a way to make here strings work in Makefiles?

P.S.: Yes, sometimes I feel autotools would be the better choice over make.

5
/bin/sh: 1: Syntax error: redirection unexpected

means you’re not using bash as your shell, in spite of your expectations to the contrary. bash as sh recognises here strings fine (so your Makefile would work on Fedora), but for example dash as sh doesn’t. Unless told otherwise, Make uses /bin/sh as its shell; it ignores your default user shell.

Setting

SHELL=/bin/bash

in your Makefile should fix things for you; at least, it does for me on a system showing the same symptoms as yours.

P.S.: Yes, sometimes I feel autotools would be the better choice over make.

Autotools and Make don’t address the same problems; they’re complementary, and using Autotools would still mean using Make...

  • It appears to me that we have autotools because doing it with make alone stops being sensible at some point. – Bananguin May 22 '18 at 12:02
  • OMG and yes. I just found the commits that removed my SHELL settings. I should have noticed by the error message though :-( – Bananguin May 22 '18 at 12:04
  • “make alone stops being sensible at some point” — agreed! Your comment gave the impression you wanted to use Autotools instead of Make. (Curious me just ran wc -l on all the Makefile.am files in my projects directory, and that said I have 82,495 lines of Automake... Oh dear...) – Stephen Kitt May 22 '18 at 12:11
4

As you can see in the error message, the shell that make invokes is not bash but /bin/sh. sh does not generally understand here-strings.

If you set the make variable SHELL to /bin/bash (or whatever the path is for that shell on your system), it will use bash instead of sh.

See also the relevant GNU make documentation about this at https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html

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.