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
  • 9,717
  • 13
  • 63
  • 119

3 Answers3

2

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

sed "1,${number}d" file
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
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
  • 33,961
  • 14
  • 109
  • 164