Just posting this here as an example for others to save the time I just lost.
test_setvar.sql file contains the following
-- :r test_setvar.sql
:reset
:setvar Name "filename"
print '$(Name)'
-- :setvar OutName '$(Name)' -- NO wont work
-- :out $(OutName).txt
:r $(Name).sql -- filename.sql exists and prints "Hello World"
:out $(Name).txt -- filename.txt is created
go
:out stdout
Stated clearly here.
Note:
Double quotes needed for multi-part strings and fields with :out
For example
:out "string bit"$(field1)"string bit2"$(field2)
Single quotes wrapping wont work with :out for this.
But...
Double or single quote wrapping is needed within a query.
where col.name LIKE "%" + "$(SEARCH_STRING)" + "%"