vi is a screen editor; you'd be better off using ex or ed. Personally I tend to use ed for such simple tasks. Also, your regex replacement is incorrect. EDITOR="/usr/bin/ex -" /usr/local/bin/sudo crontab -e tabstst<<EOF 1,$g/^.*$script11/s//#&/ wq! EOF - todd