cie
cie

Reputation: 115

How to use hex escapes in bash eval string?

I am trying to execute a command in a string that contains hexadecimal escape sequences such as \x20.

For example, if the string is ls\x20/usr/bin/, then I want to run the command ls /usr/bin/.

How can I do this?

Upvotes: 4

Views: 6020

Answers (1)

I-V
I-V

Reputation: 753

Well the readable way contains 3 lines:

CMD="ls\x20/usr/bin/"
OUTPUT="$(echo -e $CMD)"
eval "${OUTPUT}"

the echo -e will convert the string and the converted string will be stored in OUTPUT. then just use eval :)

Hope it will help you

In one line (just as you said):

$(echo -e "ls\x20/usr/bin/")

Note: this way is one line but it doesn't work with everything.. It won't work with aliases for example.

Upvotes: 4

Related Questions