Bash - command works in console but not in script

810 Views Asked by At

I have a command: printf "\x$(printf %x $char)". When I write in console: char=65; printf "\x$(printf %x $char)"; the result is written letter A. But when I run my script:

#!/bin/bash
char=65;
printf "\x$(printf %x $char)"

The result is written: \x41 I tried to write sudo before the command printf in the script and then the command works as expected. I need to find a solution without the need of using sudo.

1

There are 1 best solutions below

0
On

By running sh script.sh you accidentally run your script using dash instead of bash, whose builtin printf does not support \xHH literals.