Unprintable characters as input in command line

9,222

Solution 1

this command did the job

printf '\x0f\x11' | program

Solution 2

The printf utility supports octal character escapes:

  1. In addition to the escape sequences shown in XBD File Format Notation ( '\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v' ), "\ddd", where ddd is a one, two, or three-digit octal number, shall be written as a byte with the numeric value specified by the octal number.

The builtin printf in at least Bash and Zsh also supports hexadecimal escape sequences \xHH and also the $'...' quotes, that interpolate the escape sequences similarly.

So to use such characters in command line arguments to some command, do something like this:

$ somecmd "arg_with_$(printf '\017')_xoff"

or put it in a variable if you need to use the character more than once

$ xoff=$(printf '\017')
$ somecmd "many${xoff}odd${xoff}chars"

Or, to pipe the two bytes 0x0f 0x11 (017 021 in octal) to a command as mentioned in the comments:

$ printf "\x0f\x11" | somecmd...
$ printf "\017\021" | somecmd...

Solution 3

This depends on the shell; ZSH offers a $\NNN form

% perl -e 'printf "%vx\n", shift' $'\177'
7f
% 

In both zsh and ksh another option would be to use control+v and then return which should insert a ^M onto the command line, though this can be tedious and may overly complicate the shell history.

Lacking these, another option is to run a program that emits the appropriate characters

% perl -e 'printf "%vx\n", shift' $( perl -e 'print "\x0F"' )
f
% 

Or to use a small C program with the appropriate arguments for your program; this will avoid any complications the shell might add

#include <err.h>
#include <unistd.h>

int main(void)
{
    execlp("perl", "perl", "-e", "printf \"%vx\\n\", shift", "\x0F",
           (char *) 0);
    err(1, "exec failed");
    return 1;
}
Share:
9,222

Related videos on Youtube

FargolK
Author by

FargolK

hmm ...

Updated on September 18, 2022

Comments

  • FargolK
    FargolK over 1 year

    I've got a program that requires some unprintable characters like 0x0F (ctrl+o) as input to execute the intended part of the program.

    0       000     00              NUL     &#000;          Null char 
    1       001     01              SOH     &#001;          Start of Heading
    2       002     02              STX     &#002;          Start of Text
    3       003     03              ETX     &#003;          End of Text
    4       004     04              EOT     &#004;          End of 
    

    And it is being executed on command line, any idea how to insert unprintable chars to command line as input of a program?

    • tripleee
      tripleee almost 7 years
      The listing of ASCII control characters doesn't really belong in this question. The precise input required by the program (command-line parameters? Standard input? Something else?) would be a useful addition, though.
  • FargolK
    FargolK almost 7 years
    hmm, ok then how can I insert ascii of 0x11 for example as input of a program in linux shell?
  • FargolK
    FargolK almost 7 years
    how can I use your suggestion to covert 0x0f 0x11 to values that can be given as input to the program in shell?
  • tripleee
    tripleee almost 7 years
    Depends on the shell, but the zsh notation mentioned in this answer is available in Bash (the default shell on many distros) as well; $'\x11'
  • tripleee
    tripleee almost 7 years
    Concretely, printf '\x0f\x11' | program to pass these two bytes as standard input to program
  • FargolK
    FargolK almost 7 years
    @tripleee yup that'a right
  • Stéphane Chazelas
    Stéphane Chazelas almost 7 years
    @triplee, $'...' actually comes from ksh93 (while the $'\uXXXX' variant comes from zsh). It's now also available in zsh, bash, FreeBSD sh and mksh (and will probably be in the next major version of the POSIX standard).
  • FargolK
    FargolK almost 7 years
    @tripleee write your solution as answer not comment, to be accepted as answer
  • tripleee
    tripleee almost 7 years
    I only clarified what was already implied here, and with the latest edit, it's explicitly in this answer now. You shoud aocept this answer.
  • Pourko
    Pourko over 3 years
    Your xoff=$(printf '\017') can be done as xoff=$'\017'