ASCII underline escape code for Linux terminal capabilities - linux

In a Linux terminal emulator with xterm capabilities, printing the escape code \x1b[4m will make the following characters print with an underline.
In the Linux console with linux term capabilities, printing the escape code \x1b[4m causes the following characters to be printed in blue.
How do I print underlined text in a Linux console?
When I mention "linux" or "xterm" term capabilities I'm talking about the output of $echo $TERM.

Instead of \x1b[4m, try $(tput smul)
printf "%s\n" "$(tput smul)This is underlined"

Related

tput doesn't set color in the terminal

I wish to colorize my terminal while using interactive shell, namely the command prompt. I managed to do this using escape sequences:
PS1="\e[93m prompt> \e[0m"
I want to use tput instead of escape sequences. Like that:
PS1="$(tput setaf 1) prompt> "
However, this doesn't yeld any color.
I tried other tput subcommands in different ways. None of them gave any effect except of tput cols and tput lines which gave appropriate output.
How should I use tput properly in this case?

On Linux, why can't 'echo' use ANSI color codes but 'print' could? [duplicate]

This question already has answers here:
Bash echo command not making use of escaped character
(4 answers)
Closed 6 years ago.
As practice, I made a shell script that outputs a colored message on the screen. I was trying to use ANSI color codes but the content itself would output instead.
My code:
#!/bin/bash
echo "\033[1;37;42m SUCCESS! \033[0m"
Output:
\033[1;37;42m SUCCESS! \033[0m
However, using 'print' does work:
#!/bin/bash
print "\033[1;37;42m SUCCESS! \033[0m \n"
Output (with white font + green background):
SUCCESS!
I tested this on Red Hat Enterprise Linux Server release 6.5 (Santiago) and Raspbian Jessie Lite 4.4, and had the same results. To the best of my knowledge and from all the search engine results I've gone through, 'echo' and 'print' are the same except 'echo' includes a newline and 'print' does not. Why in this case would it be different?
By default, echo will interpret it as a string, literally. You have to tell echo to interpret backslashes appropriately.
From the documentation:
-e enable interpretation of backslash escapes
echo -e "\033[1;37;42m SUCCESS! \033[0m"

Disabling echo service in Linux

Please, explain me what is echo service in Linux and how can I disable it?
For example, now I can type "echo hello" and it will print in the terminal "hello".
Thanks.
echo is a terminal command that simply prints its arguments to the console, as you observe.
man is another command which gives instructions on usage of a command. It stands for 'manual'. Therefore, to learn more about the echo command, you can run man echo.
It is not recommended to remove the echo command from your system, since it is part of GNU Coreutils, which is an essential part of the system. If you remove it, there is a high possiblity you may break your system.
However, you may remove it with:
$ su
# rm /bin/echo
The su command will prompt for the administrator password, and if correctly given, will put you in root. This is required because 'normal' users do not usually have permission to remove essential system components.
You can exit root with the exit command.
echo is a command used to print text to stdout. Normally this just means writing to your terminal as you mention:
$ echo 'Hello world'
Hello world
For more information on this command, you can read the manpage. Here is the output from my machine:
$ man echo
NAME
echo - echo - display a line of text
echo - display a line of text
Synopsis
echo [STRING]
Description
echo displays a string of text.
The following options are available:
· -n, Do not output a newline
· -s, Do not separate arguments with spaces
· -E, Disable interpretation of backslash escapes (default)
· -e, Enable interpretation of backslash escapes
· -h, --help Display this help
Escape Sequences
If -e is used, the following sequences are recognized:
· \\ backslash
· \a alert (BEL)
· \b backspace
· \c produce no further output
· \e escape
· \f form feed
· \n new line
· \r carriage return
· \t horizontal tab
· \v vertical tab
· \0NNN byte with octal value NNN (1 to 3 digits)
· \xHH byte with hexadecimal value HH (1 to 2 digits)
Example
echo 'Hello World' Print hello world to stdout
echo -e 'Top\nBottom' Print Top and Bottom on separate lines, using an escape sequence
As far as disabling it, I can't think of any good reason to do that. As was mentioned in the comments, you could delete or move the binary so that the command cannot be called, but that just seems to be a bad bad bad idea, so I won't even mention how to do that here.

Make color escape codes work in cgdb

I have the following function residing in ~/.gdbinit:
define foo
echo \033[34m
echo testing...\n
echo \033[0m
end
When running foo in gdb it prints testing... in blue, however, when running it in cgdb the result is:
[34mtesting...
[0m
How can I enable color escape codes in cgdb?
Unfortunately, the gdb "window" in cgdb is not a full-fledged terminal... it can handle basic text I/O but it will not render any terminal escape sequences such as colors, cursor movement, etc.

Is it possible to make stdout and stderr output be of different colors in XTerm or Konsole?

Is it even achievable?
I would like the output from a command’s stderr to be rendered in a different color than stdout (for example, in red).
I need such a modification to work with the Bash shell in the Konsole, XTerm, or GNOME Terminal terminal emulators on Linux.
Here's a solution that combines some of the good ideas already presented.
Create a function in a bash script:
color() ( set -o pipefail; "$#" 2>&1>&3 | sed $'s,.*,\e[31m&\e[m,' >&2 ) 3>&1
Use it like this:
$ color command -program -args
It will show the command's stderr in red.
Keep reading for an explanation of how it works. There are some interesting features demonstrated by this command.
color()... — Creates a bash function called color.
set -o pipefail — This is a shell option that preserves the error return code of a command whose output is piped into another command. This is done in a subshell, which is created by the parentheses, so as not to change the pipefail option in the outer shell.
"$#" — Executes the arguments to the function as a new command. "$#" is equivalent to "$1" "$2" ...
2>&1 — Redirects the stderr of the command to stdout so that it becomes sed's stdin.
>&3 — Shorthand for 1>&3, this redirects stdout to a new temporary file descriptor 3. 3 gets routed back into stdout later.
sed ... — Because of the redirects above, sed's stdin is the stderr of the executed command. Its function is to surround each line with color codes.
$'...' A bash construct that causes it to understand backslash-escaped characters
.* — Matches the entire line.
\e[31m — The ANSI escape sequence that causes the following characters to be red
& — The sed replace character that expands to the entire matched string (the entire line in this case).
\e[m — The ANSI escape sequence that resets the color.
>&2 — Shorthand for 1>&2, this redirects sed's stdout to stderr.
3>&1 — Redirects the temporary file descriptor 3 back into stdout.
Here's an extension of the same concept that also makes STDOUT green:
function stdred() (
set -o pipefail;
(
"$#" 2>&1>&3 |
sed $'s,.*,\e[31m&\e[m,' >&2
) 3>&1 |
sed $'s,.*,\e[32m&\e[m,'
)
You can also check out stderred: https://github.com/sickill/stderred
I can't see that there is any way for the terminal emulator to do this.
The interface between the terminal emulator and the shell/app is via a pseudo-tty, where the terminal emulator is on the master side and the shell/app on the other. The shell/app have both stdout and stderr connected to the same pty, so when the terminal emulator reads from the pty for the shell/app output it can no longer tell which was written to stdout and which to stderr.
You will have to use one of the solutions that intercepts the data between the application and the slave-pty and inserts escape codes to control the terminal output colo(u)r.
Here is a little Awk script that will print everything you pass it in red.
#!/usr/bin/awk -f
{ printf("%c[%dm%s%c[0m\n", 0x1B, 31, $0, 0x1B); fflush() }
It simply prints each line it receives on stdin within the necessary escape codes to display it in red. It is followed by an escape code to reset the terminal.
(If you need a different color, change the second argument in the above printf call from 31 to the number corresponding to the desired color.)
Save it to colr.awk, do a chmod a+x, and use it like so:
$ my_program | ./colr.awk
It has the drawback that lines may not be displayed in order, because stderr goes directly to the console, while stdout is piped through an additional process.
A simple solution to color stdout in red is to pipe it through grep:
program | grep .
This should not require installing anything, as grep should be already installed everywhere.
Taken from Dennis’s comment on superuser.com.
I think you should use the standard escape sequences on stderr. Have a look at this.
Hilite will do this. It's a lightweight solution, but you have to invoke it for each command, eg. hilite gcc myprog.c. A more radical approach is built in to my experimental shell Gush which shows stderr from all commands run in red, stdout in black. Either way is very useful for software builds where you have lots of output with a few error messages that could easily be missed if not highlighted.

Resources