How do I print a multiline make variable directly into a pipeline?

Consider a makefile with a multiline item:

define ITEM
line $$ONE
line $$TWO
endef

item:
  echo '$(ITEM)' | ... > $@

The problem is that as ITEM gets complex, I run the risk of getting quote errors and other problems when I echo the item.

I could also run with an export:

export ITEM
item:
  print "%sn" '$${ITEM}' | ...

But I am not sure that is going to have the same bugs.

Is there some make function that will inject the contents of a variable directly into the shell, as if it were sent to /dev/stdin?

# e.g. something like:
$(info $ITEM) | ...

I understand that I can write the value directly to a file, which I’ll be able to process via shell. But being able to write it directly to a pipeline would short circuit that.

  • this will have a series of commands, or introduce requirements like sponge
  • it may also cause me to have to shift from simple envsubst to heavier things like sed to allow in-place modification (to avoid installing sponge)

I also understand I can export the value to all subshells:

  • this may be problematic if my extension is included in a recursive make project, or a make project with a large amount of export-overhead (e.g. parameter?=vars that default to env values introduce a large amount of overhead when .SECONDEXPANSION is enabled)
  • in particular, exporting too much can break the performance of zsh tab completion for make, which is a soft requirement for mac users

But I have no proof exporting and echo "$$ITEM" or print("%s/n") "$$ITEM" will have proper escapes. So I’ll have to test it.


What is not “a way”, but the intended best practice for doing this?

If the answer is: use $(file ..), and modify the file in place from there, then so be it; just want to make sure that is optimal.

7

The usual solution for a complex variable expansion is to just write an external shell file. Or a script (perl/python/awk). Or even a special compiled tool to produce something which is needed for actual compilation.
It is not a must that everything would be part of makefile.

Rule of thumb here is: “if you struggling with variables, dollar signs, and quotation marks – maybe it is time to think of external script?”

2

After reviewing the accepted answer, I am going to adapt the convention from the manual:

For example, the file function can be useful if your build system has a limited command line size and your recipe runs a command that can accept arguments from a file as well. Many commands use the convention that an argument prefixed with an @ specifies a file containing more arguments. Then you might write your recipe in this way:

program: $(OBJECTS)
        $(file >[email protected],$^)
        $(CMD) $(CMDFLAGS) @[email protected]
        @rm [email protected]

And in my case, the intermediate file I’ll have to create to stay simple with envsubst (rather than sed) will become the analog for the [email protected]:

define new-file.sh
#!/bin/bash
echo "$$FOO"
endef
 
new-file.sh:
  $(file >[email protected],$($@))
  cat [email protected] | ... > $@
  @rm [email protected]

Which will be something I’ll have a good chance of doing the same way next time.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật