How do I avoid special characters in a variable when using a makefile?

Suppose I have a makefile with show method

show::
echo $(VAR)

      

It did the output foobar

on run make VAR=foobar show

as expected.

However, when VAR is some kind of hashstring such as $2y$10$Gae9mVS

, things go wrong.

make VAR=$2y$10$Gae9mVS show

shows y0 but not $2y$10$Gae9mVS

Can someone please guide me? Thank.

+3


source to share


2 answers


You have two instances of the problem. None of them have anything to do with make.

Give it a try echo VAR=$2y$10$Gae9mVS

and see what you get.

Try it now echo 'VAR=$2y$10$Gae9mVS'

.

Then try it make 'VAR=$2y$10$Gae9mVS' show

.

Then add single quotes to the string echo '$(VAR)'

and try again make 'VAR=$2y$10$Gae9mVS' show

.

In short:



Enter your variables.

As pointed out in the comments, there is another problem here, and this problem is really related to what it does.

When you ask make to expand $(VAR)

, it expands the variables recursively and sees $

the value as expandable variables.

You can use echo '$(value VAR)'

to avoid it, or use make 'VAR=$$2y$$10$$Gae9mVS' show

to piss $

off yourself.

Unfortunately, I'm not sure if you can do this in a transparent way. This means that you need to know how the variable will be used (although any of them used in make itself will have this expansion problem) or know that your variable should not be recursively expanded on the usage site.

0


source


It will do what you get with

$ VAR=$2y$10$Gae9mVS && echo $VAR
y0

      



This happens at the shell level; single quote of a string.

$ VAR='$2y$10$Gae9mVS' && echo $VAR
$2y$10$Gae9mVS

      

0


source







All Articles