Unprintable characters as input in command line
Solution 1
this command did the job
printf '\x0f\x11' | program
Solution 2
The printf
utility supports octal character escapes:
- 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;
}
Related videos on Youtube
Comments
-
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 � Null char 1 001 01 SOH  Start of Heading 2 002 02 STX  Start of Text 3 003 03 ETX  End of Text 4 004 04 EOT  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 almost 7 yearsThe 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 almost 7 yearshmm, ok then how can I insert ascii of 0x11 for example as input of a program in linux shell?
-
FargolK almost 7 yearshow can I use your suggestion to covert 0x0f 0x11 to values that can be given as input to the program in shell?
-
tripleee almost 7 yearsDepends 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 almost 7 yearsConcretely,
printf '\x0f\x11' | program
to pass these two bytes as standard input toprogram
-
FargolK almost 7 years@tripleee yup that'a right
-
Stéphane Chazelas almost 7 years@triplee,
$'...'
actually comes from ksh93 (while the$'\uXXXX'
variant comes fromzsh
). It's now also available inzsh
,bash
, FreeBSDsh
andmksh
(and will probably be in the next major version of the POSIX standard). -
FargolK almost 7 years@tripleee write your solution as answer not comment, to be accepted as answer
-
tripleee almost 7 yearsI 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 over 3 yearsYour
xoff=$(printf '\017')
can be done asxoff=$'\017'