Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this command to copy certain line from one file to another.Its working fine.No issue with it.

sed -f <(sed -e '1,10d; 12,$d; x; s/.*/10a\/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt

The problem is instead of 10, I want to use variable VAR1 (where var1=10). The $VAR1 is not working.

I tried this command:

sed -f <(sed -e '1,$VAR1d; 12,$d; x; s/.*/10a\/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt

Please help me.

share|improve this question
Please ask shortcut of your question inside question field. And add anything related to that question inside description field. – Danatela Sep 30 '13 at 10:35
up vote 7 down vote accepted

The shell doesn't expand variables inside single quotes. You need to use double quotes. Also, as Danatela says, you also need curly brackets in this case. Since the shell will then attempt to expand the $d too, you need to escape the $.

sed -f <(sed -e "1,${VAR1}d; 12,\$d; x; s/.*/10a\/;p; x" ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt

I'm not sure if there are other parts in the quotes that will also need escaping since you use double quotes now (e.g. *?), so you can always switch between double and single quotes instead, using the former only when necessary.

sed -f <(sed -e '1,'"${VAR1}"'d; 12,$d; x; s/.*/10a\/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt
share|improve this answer

You should use curly braces around variable name:

sed -f <(sed -e '1,${VAR1}d; 12,$d; x; s/.*/10a/;p; x' ../log/file2.txt ) ../log/file4.txt > ../log/file5.txt

Thus $ will evaluate VAR1 instead of VAR1d.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.