32

How can I list all the printable ASCII characters in the terminal?

10 Answers 10

40

You can do:

man ascii

to see the whole set of ascii characters, or you can just run the command ascii.

$ ascii
Usage: ascii [-dxohv] [-t] [char-alias...]
   -t = one-line output  -d = Decimal table  -o = octal table  -x = hex table
   -h = This help screen -v = version information
Prints all aliases of an ASCII character. Args may be chars, C \-escapes,
English names, ^-escapes, ASCII mnemonics, or numerics in decimal/octal/hex.

Dec Hex    Dec Hex    Dec Hex  Dec Hex  Dec Hex  Dec Hex   Dec Hex   Dec Hex  
  0 00 NUL  16 10 DLE  32 20    48 30 0  64 40 @  80 50 P   96 60 `  112 70 p
  1 01 SOH  17 11 DC1  33 21 !  49 31 1  65 41 A  81 51 Q   97 61 a  113 71 q
  2 02 STX  18 12 DC2  34 22 "  50 32 2  66 42 B  82 52 R   98 62 b  114 72 r
  3 03 ETX  19 13 DC3  35 23 #  51 33 3  67 43 C  83 53 S   99 63 c  115 73 s
  4 04 EOT  20 14 DC4  36 24 $  52 34 4  68 44 D  84 54 T  100 64 d  116 74 t
  5 05 ENQ  21 15 NAK  37 25 %  53 35 5  69 45 E  85 55 U  101 65 e  117 75 u
  6 06 ACK  22 16 SYN  38 26 &  54 36 6  70 46 F  86 56 V  102 66 f  118 76 v
  7 07 BEL  23 17 ETB  39 27 '  55 37 7  71 47 G  87 57 W  103 67 g  119 77 w
  8 08 BS   24 18 CAN  40 28 (  56 38 8  72 48 H  88 58 X  104 68 h  120 78 x
  9 09 HT   25 19 EM   41 29 )  57 39 9  73 49 I  89 59 Y  105 69 i  121 79 y
 10 0A LF   26 1A SUB  42 2A *  58 3A :  74 4A J  90 5A Z  106 6A j  122 7A z
 11 0B VT   27 1B ESC  43 2B +  59 3B ;  75 4B K  91 5B [  107 6B k  123 7B {
 12 0C FF   28 1C FS   44 2C ,  60 3C <  76 4C L  92 5C \  108 6C l  124 7C |
 13 0D CR   29 1D GS   45 2D -  61 3D =  77 4D M  93 5D ]  109 6D m  125 7D }
 14 0E SO   30 1E RS   46 2E .  62 3E >  78 4E N  94 5E ^  110 6E n  126 7E ~
 15 0F SI   31 1F US   47 2F /  63 3F ?  79 4F O  95 5F _  111 6F o  127 7F DEL
6
  • 1
    or just ascii.
    – jmtd
    Jun 17, 2011 at 9:23
  • 2
    not available in macOS Sierra, it seems Apr 25, 2018 at 10:48
  • What Linux distro is this native on? Ubuntu? Aug 14, 2018 at 23:08
  • 1
    command ascii not available in latest Ubuntu WSL but man ascii works fine.
    – rob
    Sep 24, 2019 at 9:48
  • 1
    On macOS you can install ascii with brew e.g. brew install ascii
    – kenlukas
    Dec 13, 2019 at 3:10
24

Try with printf and a shell like ksh93, zsh or bash:

for ((i=32;i<127;i++)) do printf "\\$(printf %03o "$i")"; done;printf "\n"

See also : BASH FAQ

2
  • Or, the same idea, using a differnt way to get the octal value: for((i=32;i<=127;i++)) do printf "\\$((i/64*100+i%64/8*10+i%8))\t"; done;printf "\n" ...
    – Peter.O
    Jun 17, 2011 at 19:34
  • Or using the bash builtin echo... for((i=32;i<=127;i++)) do eval echo -ne $\'\\$((i/64*100+i%64/8*10+i%8))\'"\\\t"; done; echo
    – Peter.O
    Jun 17, 2011 at 20:40
13

More an awk solution that a (pure) shell one but here it is anyway:

awk 'BEGIN{for(i=32;i<127;i++)printf "%c",i; print}'
5

The man page ascii also can be used to get a list like so:

$ man 7 ascii
ASCII(7)                  Linux Programmer's Manual                  ASCII(7)

NAME
       ascii - ASCII character set encoded in octal, decimal, and hexadecimal

DESCRIPTION
       ASCII  is  the American Standard Code for Information Interchange. 
       It is a 7-bit code.  Many 8-bit codes (such  as  ISO  8859-1,  the  
       Linux default  character set) contain ASCII as their lower half.  
       The international counterpart of ASCII is known as ISO 646.

       The following table contains the 128 ASCII characters.

       C program '\X' escapes are noted.

       Oct   Dec   Hex   Char                        Oct   Dec   Hex  Char
       ──────────────────────────────────────────────────────────────────────
       000   0     00    NUL '\0'                    100   64    40   @
       001   1     01    SOH (start of heading)      101   65    41   A
       002   2     02    STX (start of text)         102   66    42   B
       003   3     03    ETX (end of text)           103   67    43   C
       004   4     04    EOT (end of transmission)   104   68    44   D
       005   5     05    ENQ (enquiry)               105   69    45   E
       006   6     06    ACK (acknowledge)           106   70    46   F
       007   7     07    BEL '\a' (bell)             107   71    47   G
       010   8     08    BS  '\b' (backspace)        110   72    48   H
       011   9     09    HT  '\t' (horizontal tab)   111   73    49   I
       012   10    0A    LF  '\n' (new line)         112   74    4A   J
       013   11    0B    VT  '\v' (vertical tab)     113   75    4B   K
       014   12    0C    FF  '\f' (form feed)        114   76    4C   L
       015   13    0D    CR  '\r' (carriage ret)     115   77    4D   M
       016   14    0E    SO  (shift out)             116   78    4E   N
       017   15    0F    SI  (shift in)              117   79    4F   O
       020   16    10    DLE (data link escape)      120   80    50   P
       021   17    11    DC1 (device control 1)      121   81    51   Q
       022   18    12    DC2 (device control 2)      122   82    52   R
       023   19    13    DC3 (device control 3)      123   83    53   S
       024   20    14    DC4 (device control 4)      124   84    54   T
       025   21    15    NAK (negative ack.)         125   85    55   U
       026   22    16    SYN (synchronous idle)      126   86    56   V
       027   23    17    ETB (end of trans. blk)     127   87    57   W
       030   24    18    CAN (cancel)                130   88    58   X
       031   25    19    EM  (end of medium)         131   89    59   Y
       032   26    1A    SUB (substitute)            132   90    5A   Z
       033   27    1B    ESC (escape)                133   91    5B   [
       034   28    1C    FS  (file separator)        134   92    5C   \  '\\'
       035   29    1D    GS  (group separator)       135   93    5D   ]
       036   30    1E    RS  (record separator)      136   94    5E   ^
       037   31    1F    US  (unit separator)        137   95    5F   _
       040   32    20    SPACE                       140   96    60   `
    ...
    ...
4

Expanding on jlliagre's solution (and useful if you don't have the ascii command available):

awk 'BEGIN {for (i = 32; i < 127; i++) printf "%3d 0x%02x %c\n", i, i, i}'

You can also pipe the above to pr -t6 -w78 to get a one page output:

 32 0x20      48 0x30 0    64 0x40 @    80 0x50 P    96 0x60 `   112 0x70 p
 33 0x21 !    49 0x31 1    65 0x41 A    81 0x51 Q    97 0x61 a   113 0x71 q
 34 0x22 "    50 0x32 2    66 0x42 B    82 0x52 R    98 0x62 b   114 0x72 r
 35 0x23 #    51 0x33 3    67 0x43 C    83 0x53 S    99 0x63 c   115 0x73 s
 36 0x24 $    52 0x34 4    68 0x44 D    84 0x54 T   100 0x64 d   116 0x74 t
 37 0x25 %    53 0x35 5    69 0x45 E    85 0x55 U   101 0x65 e   117 0x75 u
 38 0x26 &    54 0x36 6    70 0x46 F    86 0x56 V   102 0x66 f   118 0x76 v
 39 0x27 '    55 0x37 7    71 0x47 G    87 0x57 W   103 0x67 g   119 0x77 w
 40 0x28 (    56 0x38 8    72 0x48 H    88 0x58 X   104 0x68 h   120 0x78 x
 41 0x29 )    57 0x39 9    73 0x49 I    89 0x59 Y   105 0x69 i   121 0x79 y
 42 0x2a *    58 0x3a :    74 0x4a J    90 0x5a Z   106 0x6a j   122 0x7a z
 43 0x2b +    59 0x3b ;    75 0x4b K    91 0x5b [   107 0x6b k   123 0x7b {
 44 0x2c ,    60 0x3c <    76 0x4c L    92 0x5c \   108 0x6c l   124 0x7c |
 45 0x2d -    61 0x3d =    77 0x4d M    93 0x5d ]   109 0x6d m   125 0x7d }
 46 0x2e .    62 0x3e >    78 0x4e N    94 0x5e ^   110 0x6e n   126 0x7e ~
 47 0x2f /    63 0x3f ?    79 0x4f O    95 0x5f _   111 0x6f o

In my OS-independent startup script I have:

command -v ascii &> /dev/null || function ascii { awk 'BEGIN {for (i = 32; i < 127; i++) printf "%3d 0x%02x %c\n", i, i, i}' | pr -t6 -w78; }

(note the ; before the function's closing })

Special thanks to Stéphane Chazelas for the pr -t6 part.

0
1

With zsh:

$ print -raC16 {" "..~}
   !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /
0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?
@  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O
P  Q  R  S  T  U  V  W  X  Y  Z  [  \  ]  ^  _
`  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~
0

Using jot(1):

$ jot -c 96 32

!
"
#
$
...
{
|
}
~

This command prints 96 integers beginning at 32, and formats each integer as an ASCII character, delimited by newlines.

2
  • 1
    There are 95 (not 96) printable ascii characters. The last character from your command is a DEL (0x7f) character (not printable).
    – user232326
    Dec 19, 2019 at 19:50
  • Probably: jot -s " " -c 95 32 would be a better solution.
    – user232326
    Dec 19, 2019 at 19:51
0

With zsh's brace expansion:

$ echo {\ ..\~}
  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
2
  • Since the OP did not mention a shell, it's logical to assume bash (as it's the default). Do you have an answer for bash? The aforementioned solution does not work. Dec 13, 2019 at 2:34
  • @ajgringo619 Unfortunately I don't know any comparable method in Bash 😥. You can try the printf or awk answers which are also pretty concise.
    – Shaoyun
    Dec 13, 2019 at 3:27
0

Portably:

$ printf "$(printf \\%03o $(seq 32 126))\n" | fold -w 48
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

With an space:

$ printf "$(printf '\\%03o ' $(seq 32 126))\n" | fold -w 48
  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 
8 9 : ; < = > ? @ A B C D E F G H I J K L M N O 
P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g 
h i j k l m n o p q r s t u v w x y z { | } ~
0

Same as in existing answers with printf, but using brace-expansion and with hex-codes:

printf "$(printf '\\x%x' {32..126})\n"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .