Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing binary data as arguments in bash

I need to pass binary data to a bash program that accepts command line arguments. Is there a way to do this?

It's a program that accepts one argument:

script arg1 

But instead of the string arg1, I'd like to pass some bytes that aren't good ASCII characters - in particular, the bytes 0x02, 0xc5 and 0xd8.

How do I do this?

like image 561
Cam Avatar asked Feb 28 '12 17:02

Cam


People also ask

What is $@ in Bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do you pass an argument in shell?

Using arguments Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

How many arguments can be passed to bash script?

NUM} ) in the 2^32 range, so there is a hard limit somewhere (might vary on your machine), but Bash is so slow once you get above 2^20 arguments, that you will hit a performance limit well before you hit a hard limit.


2 Answers

script "`printf "\x02\xc5\xd8"`" script "`echo -e "\x02\xc5\xd8"`" 

test:

# echo -n "`echo -e "\x02\xc5\xd8"`" | hexdump -C 00000000  02 c5 d8                                          |...| 
like image 68
Karoly Horvath Avatar answered Sep 29 '22 08:09

Karoly Horvath


Use the $'' quote style:

script $'\x02\xc5\xd8' 

Test:

printf $'\x02\xc5\xd8' | hexdump -C 00000000  02 c5 d8 
like image 21
l0b0 Avatar answered Sep 29 '22 06:09

l0b0