0

Can you explain why

CHAR=$(printf "\x$(printf %x 42)"); echo $CHAR

works as ls command in the current directory?

I'm trying to print a character by its decimal ASCII code. "man" what and where do I need to read? Or is there some better way to get the character in decimal code?

I'm trying to get "*" in the output.

James Z
  • 12,209
  • 10
  • 24
  • 44
Sadok
  • 36
  • 2

1 Answers1

1

Unquoted variable expansion undergoes word splitting and filename expansion. Quote it.

CHAR='*'
echo "$CHAR"

why works as ls command in the current directory?

Because CHAR contains a star, unquoted $CHAR triggers filename expansion, which replaces it by paths in the current directory. Because it was designed that way.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • It's understandable. I apologise for not revealing the task further `shuf -n 12 -zre $CHAR` performs an asterisk. I've already escaped this in all sorts of ways. At this `shuf -n 12 -zre * shuf -n 12 -zre "*"` works without any problems. – Sadok Apr 09 '23 at 08:46