Lines 8.
and 9.
below confound me:
#!/bin/bash
a=foo
b=6
c=a
d="\e[33m" # opening ansi color code for yellow text
e="\e[0m" # ending ansi code
f=$d
printf "1. foo\n"
printf "2. $a\n"
printf "3. %s\n" "$a"
printf "4. %s\n" "${!c}"
printf "5. %${b}s\n" "$a"
printf "6. $d%s$e\n" "$a" # will be yellow
printf "7. $f%s$e\n" "$a" # will be yellow
printf '8. %s%s%s\n' "$d" "$a" "$e" # :(
printf "9. %s%s%s\n" "$f" "$a" "$e" # :(
Is it possible to use %s
to expand a colour variable and see the colour switch?
Output:
1. foo
2. foo
3. foo
4. foo
5. foo
6. foo
7. foo
8. \e[33mfoo\e[0m
9. \e[33mfoo\e[0m
Note: 6.
and 7.
are indeed yellow
printf "10. %b%s%b\n" "$f" "$a" "$e" # :)
... finally! That's the command that does it, thanks to Josh!
Practical Data Science using Python You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]
The code containing only 0 (being \x1B[0m ) will reset any style property of the font. Most of the time, you will print a code changing the style of your terminal, then print a certain string, and then, the reset code.
You're looking for a format specifier that will expand escape characters in the argument. Conveniently, bash supports (from help printf
):
%b expand backslash escape sequences in the corresponding argument
Alternatively, bash also supports a special mechanism by which will perform expansion of escape characters:
d=$'\e[33m'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With