yash
yash

Reputation: 1357

How to enter a backspace character on a shell command line?

I am trying to do Exercise 1-10 in K&R. I've got the program working and running. So far I've come to know that the backspace character is cooked with the operating system. How can I input the backspace character in Mac OS X?

Upvotes: 1

Views: 9615

Answers (2)

mauro
mauro

Reputation: 5950

You can use (non destructive) backspace \b in printf and re-write. This way:

$ cat w.c
#include <stdio.h>

int main () 
{
    printf("abcd\n");
    printf("abc\bd\n");
}
$ ./w
abcd
abd

UPDATE Same story using putchar():

$ cat w.c
#include <stdio.h>

int main () 
{
    printf("abcd\n");
    putchar('a');
    putchar('b');
    putchar('c');
    putchar('\b');
    putchar('d');
    putchar('\n');
}

Same output...

Upvotes: 1

mustaccio
mustaccio

Reputation: 19011

Not sure what you mean by "cooked with the operating system". I guess you're asking how to enter a backspace character on the shell command line without it being interpreted as a backspace, that is, without actually erasing the previous character.

The key combination for the ASCII backspace control character is ^H (hold down Ctrl and press H. However, pressing this combination on the shell command line simply performs the "backspace" operation. To quote a control character in Bash, which is the OS X default shell, and in Zsh you type a combination of two characters: ^V^H.

Upvotes: 6

Related Questions