Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bash variables for range in sed

Tags:

bash

sed

sed -n '5,10 p' < /proc/cpuinfo 

prints 5-10 lines of the file /proc/cpuinfo

I want to use something like

start=5
end=10
sed -n '$start,$end p' < /proc/cpuinfo

so that I can change the values of start and end form a script.

like image 415
user2647717 Avatar asked Feb 22 '26 21:02

user2647717


1 Answers

You need to use double quotes for variable expansion:

start=5 end=10; sed -n "$start,$end p" < /proc/cpuinfo
like image 192
rid Avatar answered Feb 24 '26 16:02

rid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!