How can I make the watch command interpret vt100 sequences?

前端 未结 3 1480
长情又很酷
长情又很酷 2021-01-04 00:39

Consider this simple example (which displays in red):

echo -e \"\\033[31mHello World\\033[0m\"

It displays on the terminal correctly in red. No

相关标签:
3条回答
  • 2021-01-04 00:49

    You can try single quoting your command :

    watch 'echo -e "\tHello World"'
    

    On my machine this leaves me with a -e as first character, and a correctly tabbed hello world. It seems -e is the default for my version of echo. Still, it is a progress toward a correctly tabbed hello world

    What happens is a double unquoting :
    what watch see

    echo -e "\033[31mHello World\033[0m"
    

    what the shell called by watch see :

    echo -e \033[31mHello World\033[0m
    

    And then the backslash come into play, even when quoted, and it becomes a quoting nightmare.

    0 讨论(0)
  • 2021-01-04 01:05

    Edit:

    More recent versions of watch support color. You will need to use an extra level of quoting to preserve the quotes and escapes in the particular situation of the example in the question:

    watch 'echo -e "\033[31mHello World\033[0m"'
    

    From man watch:

      -c, --color
              Interpret ANSI color sequences.
    

    Previously:

    From man watch:

    Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.

    But they don't get interpreted, so I don't think there's any way.

    0 讨论(0)
  • From man watch of watch 0.3.0 on Ubuntu 11.10:

    By default watch will normally not pass escape characters, however if you use the --c or --color option, then watch will interpret ANSI color sequences for the foreground.

    It doesn't seem to work with your literal string on my terminal, but these work:

    watch --color 'tput setaf 1; echo foo'
    watch --color ls -l --color
    
    0 讨论(0)
提交回复
热议问题