0

In my bash, I wish to delete lines from 1 to ${number}, like this:

number=10
sed '1,${number}d' myfile

It failed with syntax error. I also tried "\${number}", '"$number"', neithers works


I changed to double quote, it works under command line, but not inside bash script file. How to make it work?

Hind Forsum
  • 8,897
  • 9
  • 50
  • 103

3 Answers3

2

Enclose your sed expression in double quotes to interpolate the variable number:

sed "1,${number}d" file
RomanPerekhrest
  • 75,918
  • 4
  • 51
  • 91
2

Please use double quotes instead of single quotes while running sed this way. Read here for Difference between single and double quotes

asatsi
  • 452
  • 2
  • 5
1

I changed to double quote, it works under command line, but not inside bash script file

Assuming your bash script is:

sed "1,${number}d" myfile

you should export number variable in bash prompt so that it can be visible in bash script when it is run:

export number=10
ks1322
  • 31,484
  • 13
  • 100
  • 154