Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line echo in makefile

Tags:

bash

makefile

I'm trying to do simple c++ class generator in makefile (depending on given name), but it's really annoying when i don't know bash, and how those things work.

class:
    @echo "Type class name: "; \
    read CLASS_NAME; \
    echo Creating class called $${CLASS_NAME}; \
    echo "Class $${CLASS_NAME} {
    int x;
    int y;
    };">./$${CLASS_NAME}.h  

How can I make the last echo work? I don't know how to echo multiple lines.

like image 892
grubazhuberto Avatar asked Oct 23 '25 05:10

grubazhuberto


1 Answers

The backslashes here are evaluated by make, not by the shell it invokes. (Similarly, make removes the newlines itself, instead of passing them to the shell).

The easiest thing to do here is to use a printf format string to insert your newlines rather than trying to make them literal:

class:
        @echo "Type class name: "; \
        read CLASS_NAME; \
        echo "Creating class called $${CLASS_NAME}"; \
        printf '%s\n' "Class $${CLASS_NAME} {" 'int x;' 'int y;' '};' >"./$${CLASS_NAME}.h"
like image 92
Charles Duffy Avatar answered Oct 26 '25 07:10

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!