Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to change the font attributes of the output of echo in either zsh or bash?

What I would like is something akin to:

echo -n "This is the font: normal "
echo -n $font=italic "italic,"
echo -n $font=bold "bold,"
echo -n "and"
echo -n $font=small "small".

so that it print: "This is the font: normal, italic, bold, small" within a line of text. Note that I cannot get markdown to format "small" as a small text...

share|improve this question
    
This has more to do with the terminal emulator in question than with the used shell. This question thread contains some valuable pointers, I think –  sr_ Apr 25 '12 at 8:33
    
Normally, you cannot change the font size. The only thing, you usually can do, is changing color and sometimes the bold/underlined attribute. For this, you can use ANSI escape sequences. See e.g. bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html for some examples. –  jofel Apr 25 '12 at 8:35
add comment

3 Answers

up vote 4 down vote accepted

On most if not all terminal emulators, you can't set different font sizes or different fonts, only colors and a few attributes (bold, underlined, standout).

In bash (or in zsh or any other shell), you can use the terminal escape sequences directly (apart from a few exotic ones, all terminals follow xterm's lead these days). CSI is ESC [, written $'\e[' in bash. The escape sequence to change attributes is CSI Ps m.

echo $'\e[32;1mbold red\e[0mplain\e[4munderlined'

Zsh has a convenient function for that.

autoload -U colors
colors
echo $bold_color$fg[red]bold red${reset_color}plain$'\e'$color[underline]munderlined
share|improve this answer
add comment

You could include these color definitions in a script or source file. Could look something like this.

#!/bin/bash
PATH=/bin:/usr/bin:

NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
PURPLE='\033[01;35m'
CYAN='\033[01;36m'
WHITE='\033[01;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'

echo -e "This text is ${RED}red${NONE} and ${GREEN}green${NONE} and ${BOLD}bold${NONE} and ${UNDERLINE}underlined${NONE}."

tput sgr0

Notice that you should reset the ANSI color codes after each instance you invoke a change. The tput sgr0 resets all changes you have made in the terminal.

I believe changing the font size or italics would be specific to the terminal you are using.

While this guide is specific to customizing your bash prompt, it is a good reference for color codes and generates some ideas of what you can do.

share|improve this answer
    
+1: exactly what I needed ! –  Stephane Rolland Feb 7 '13 at 23:47
add comment

Seems as if the layout can't handle the [0x1b]-character in front of the [.

The first line makes bold:

 echo -e "\x1b[1m bold"
     echo -e "\x1b[30m black"
     echo -e "\x1b[31m red"
     echo -e "\x1b[32m green"
     echo -e "\x1b[33m yellow"
     echo -e "\x1b[34m blue"
     echo -e "\x1b[35m mag"
     echo -e "\x1b[36m cyan"
     echo -e "\x1b[37m white"   

For the general type, I only know

echo -e "\x1b[0m io-std"
echo -e "\x1b[1m bold"
echo -e "\x1b[2m normal"

and from the comments, thanks manatwork:

echo -e "\x1b[4m underlined"
echo -e "\x1b[5m blinking"
echo -e "\x1b[7m inverted"

and don't know the difference between io-std and normal.

I haven't seen italic or small in the shell.

You can enter them (thanks to manatwork too) by Ctrl + v ESC in the Bash, where it will be displayed as ^[. This mean the whole ANSI sequence will look like ^[[1m bold or ^[[1mbold (to avoid the blank before 'bold').

Many editors have problems with (char)0x1b. Alternatives: copy/paste it from somewhere, or use

echo -e "\x1b[1m bold"

in bash, or a hex editor.

share|improve this answer
1  
Escape characters can be typed: Ctrl+V, Esc. (Will be displayed as “^[”. This mean the whole ANSI sequence will look like “^[[1mbold”.) –  manatwork Apr 25 '12 at 11:49
1  
There are more attributes to mention: \e[4m underlined, \e[5m blinking, \e[7m inverted. –  manatwork Apr 25 '12 at 11:52
    
@manatwork: added them. –  user unknown Apr 25 '12 at 12:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.