Why is my sed command not working when using variables?

using bash, I am trying to insert a variable for a date and look for a log file for that date, and then send the output to a file. If I hardcode a date like this, it works:

sed -n '/Nov 22, 2010/,$p' $file >$log_file

but if I do it like this, it fails:

date="Nov 22, 2010"
sed -n '/$date/,$p' $file >$log_file

The error I get: sed: 1: "/Nov 22, 2010/,": expected context address Thanks for the help.

+3
source share
1 answer

In shell scripts, there is a difference between single and double quotes. Variables inside single quotes do not expand (unlike double quotes), so you will need:

date="Nov 22, 2010"
sed -n "/$date/,\$p" $file >$log_file

, sed.

+7

Source: https://habr.com/ru/post/1776362/


All Articles