The octave system does not interpret the escape sequence
When executing this command from prompt or from script
system ("printf ""\\x1B[32mword\\x1B[0m\n""");
it will print this text
\x1B[32mword\x1B[0m
instead of interpreting the escape sequence for the printf
linux command . Octave unix
will raise the same result.
The command itself is correctly interpreted from the Linux command line, printing the word "word" in green.
How do I get the color escape sequence to be interpreted instead of raw text?
Powered by GNU Octave version 3.6.1.
Update: It seems that Octave does not support these escape sequences ( link ) even though GNU pages ( link ). The correct command would be
system ("printf ""\x1B[32mejemplo\x1B[0m\n""");
and its exit
warning: unrecognized escape sequence \x' -- converting to x'
warning: unrecognized escape sequence \x' -- converting to x'
x1B[32mejemplox1B[0m
Update: . In the meantime, I have solved the problem with this bash script, which can certainly be caused by the call system
.
#!/bin/bash
#
# formatted output
# arguments:
#
# Text attributes
# 0 All attributes off
# 1 Bold on
# 4 Underscore (on monochrome display adapter only)
# 5 Blink on
# 7 Reverse video on
# 8 Concealed on
#
# Foreground colors
# 30 Black
# 31 Red
# 32 Green
# 33 Yellow
# 34 Blue
# 35 Magenta
# 36 Cyan
# 37 White
#
# Background colors
# 40 Black
# 41 Red
# 42 Green
# 43 Yellow
# 44 Blue
# 45 Magenta
# 46 Cyan
# 47 White
if [[ "$#" -ne 4 ]]; then
echo "cprintf (bash): wrong number of parameters."
exit 1
fi
printf "\x1b[%d;%d;%dm%s\x1b[0m" $1 $2 $3 $4
source to share
Octave interprets double quotes . And your octave version doesn't understand \x
. The solution is to use the single quote character as
In single-quoted strings, the backslash is not a special character
And let bash handle the color highlighting sequence:
system('bash -c ''printf "\x1B[32mword\x1B[0m\n"''');
Duplicate single quotes are required for the command launched by the system to be
bash -c 'printf "\x1B[32mword\x1B[0m\n"'
Executes a command printf "\x1B[32mword\x1B[0m\n"
inside a new shell bash
. And the escape characters in the string between double quotes are interpreted correctly.
As a side note, with octave 3.8.2 on linux you can just issue
system('printf "\x1B[32mword\x1B[0m\n"');
or even
printf "\x1B[32mword\x1B[0m\n"
source to share