1

I am trying to programmatically create a file by printf-ing different things to it. (i.e. printf %s\\n hostname >> file.txt)

I would like to send an Esc code to clear the screen (it would be the first line).

In bash, I just use printf $'\033[2J'$'\033[;H' (\033 being the octet for Esc) and it clears the screen. Everything works as it should; as the first line in the file, when you cat the file, it clears the screen first.

In tcsh (the default shell for FreeBSD root), I can't seem to figure out how to "escape the escape" and get the equivalent of the bash escape codes.

I have been experimenting with (printf and echo)

  • echo %{\e[2J]}
  • echo \e
  • echo \\e
  • echo %{\033[2J}

If anyone can point me in the right direction, I would be very appreciative!.

Thanks!

5

printf expands those in the format (first) argument by itself, no need for those ksh93-style $'...' quotes. So:

printf '\33[2J\33[H'

Note that printf is not a builtin in tcsh, so you'd be calling the printf command on the filesystem. You might as well call the tput or clear commands then, but in tcsh that's not needed as tcsh has built-in support for termcap/terminfo, so you can do:

echotc clear

That will query the terminfo or termcap database for the right escape sequence to send for the current terminal (according to $TERM) which is usually better than using a hard-coded one.

If you wanted to use tcsh's echo built-in, you could do:

set echo_style = both # meant to be the default in tcsh
echo -n '\033[2J\033[H'

Or:

set echo_style = sysv # or both
echo '\033[2J\033[H\c'
  • This answered my question perfectly, but (trying not to ask another question in this topic)..I am curious by what you mean printf is not built in to tcsh because I have a virgin FreeBSD machine and printf works. – Allan Mar 14 '17 at 17:44
  • @Allan, compare which echo (or which echotc) vs which printf at the prompt of tcsh. – Stéphane Chazelas Mar 14 '17 at 17:46
  • Understood. I appreciate the help. – Allan Mar 14 '17 at 17:51

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.