echo -n "##### STATIONS? #######"
read station
text="stab_site "

I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work,

sed -i "27i\$text$station" text.data

it produce; $textankr ista "text" variable cannot be passed within sed function.

2 Answers 2


That's because your shell is interpreting \$ to mean a literal (escaped) dollar sign. Try

sed -i "27i\\$text$station" text.data

or using a mix of single- and double-quotes

sed -i '27i\'"$text$station" text.data

or with a literal newline

sed -i "27i\
$text$station" text.data

(which is more POSIXly correct, I think).

  • how can I give one space after the beginning of the written line? Dec 13, 2015 at 16:36
  • @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station Dec 13, 2015 at 16:42

Maybe the problem can be a empty output file. Example: #!/bin/bash


RESULTADO=`date +%d/%m/%Y | tr '\n' ' '; dropbox status;`

sed -i "1i\
$RESULTADO" /etc/scripts/dropbox.log

This script cant write to a empty file dropbox.log.

Write some text on file and execute script again.

  • The problem was solved with the other answer; why would it be caused by an empty output file? Jan 14, 2019 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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