1

I want to add a sentence to header of a file using bash script, I tried

echo "#define Block $i">>mypro.cpp

but it add '#define Block $i' to it's end. Thanks in advance

Luminous
  • 13
  • 1
  • 3

1 Answers1

1

You can used sed :

[romano:~/tmp] % echo test >! test.c       
[romano:~/tmp] % sed -i -e "1i #include" test.c
[romano:~/tmp] % cat test.c 
#include
test

...just take care of the correct quoting.

More verbose: sed -i means to edit the file (the last argument) in place; the command "NiSomething" insert Something at line N.

PD this works on Linux and (GNU sed) 4.2.2 --- POSIX sed require a stricter syntax.

Rmano
  • 31,947