Allan Thomas
Allan Thomas

Reputation: 3589

Using a bash variable in regex replace

replace="bar"
echo foo123 | perl -p -e 's/(\d+)/${replace}456/'

Is there a way to use a pre-defined variable in your replacement string? Maybe even a better alternative to perl?

Upvotes: 0

Views: 286

Answers (1)

JRFerguson
JRFerguson

Reputation: 7516

replace="bar"  
echo foo123 | perl -p -e 's/(\d+)/$ENV{replace}456/'

This grabs the shell variable from the environment in which Perl runs. In some circumstances you might need to export first:

export replace="bar"

Upvotes: 4

Related Questions