Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wanted to output a string of all the ascii characters with the following command

for i in `seq 32 127`; do printf "%c" $i; done

The output of the above command is:


It's the first (from the left) digit of each number.

Looking through this site I came across the answer to my problem How to print all printable ASCII chars in CLI?, however I still don't understand why my original snippet does not output the ascii characters as intended.

share|improve this question
POSIX dictates this, here's a comp.unix.shell thread on why it's the right thing ;) – sr_ Aug 4 '12 at 11:36
@sr_ Thanks for pointing out the thread. It had the explanation I was looking for. – Ifthikhan Aug 4 '12 at 18:06
up vote 8 down vote accepted

You can't directly print the ascii codes by using the printf "%c" $i like in C.

You have to first convert the decimal value of i into its octal value and then you have to print it using using printf and putting \ in front of their respective octal values.

To print A, you have to convert the decimal 65 into octal, i.e. 101, and then you have to print that octal value as:

printf "\101\n"

This will print A.

So you have to modify it to :

for i in `seq 32 127`; do printf \\$(printf "%o" $i);done;

But by using awk you can directly print like in C language

awk 'BEGIN{for(i=32;i<=127;i++)printf "%c",i}';echo
share|improve this answer
In bash and zsh this can be done without the loop and without the external command: printf $(printf '\%o' {32..127}). – manatwork Aug 4 '12 at 12:02
@manatwork: ya exactly..thanks a lot for pointing it out.. – pradeepchhetri Aug 4 '12 at 12:08
@pradeepchhetri : Thank you for the detailed response and it seemed to cover most of the required details (hence I choose your answer). However I guess it missed out an important piece of information which can be found in the following message at unix.derkeiler.com/Newsgroups/comp.unix.shell/2007-07/…. It states that "The argument operands shall be treated as strings if the corresponding conversion specifier is b, c, or s..." – Ifthikhan Aug 4 '12 at 18:11
Isn't (char)(127) backspace or something like that. Whatever it is, it appears as one of the hex-boxes or whatever they are called. If you want only the "printable" (i.e. readable), just go to 126. Also, nice thought with the octal. That's clever; I was thinking in terms of hexadecimal (like printf '\x%x; {32..126} ... or 127, I guess, since all of you did it, too), but it doesn't work. Octal saves the day! :) Finally, @Ifthikhan, I'm not sure what you mean. awk often uses C-style commands and nowhere else is %c used. Using octal numbers is different than using one-byte characters. – Dylan Nov 20 '14 at 14:31

%c Interprets the associated argument as char: only the first character of a given argument is printed

You seem to already have a way to print them, but here is one variant.

for i in `seq 32 127`; do printf "\x$(printf "%x" $i) $i"; done
share|improve this answer

Your Answer


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.