0
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?

1 Answer 1

4
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"
Sign up to request clarification or add additional context in comments.

1 Comment

Alternatively, if you don't want to export replace to the environment, echo foo123 | replace="$replace" perl -p -e 's/(\d+)/$ENV{replace}456/'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.