It takes a decimal number, converts it into an octal representation, then interprets it as a character and store it into a variable.
More detailed description (see printf(1) man for better explanation):
- the inner
printf "%o" "$d"
prints an octal number based on a decimal number $d
(e. g. if $d
is 65
, it prints 101
)
$()
means command substition, i. e. instead of printing, the octal number (101
) is going to be substituted into the shell command itself
\
prepends a backslash to the number (e. g. 101
) (double backslash must be used, as single backslash would have disabled command substitution $()
)
- after the first substitution, the whole command looks like so:
char=$(printf 101)
N
is recognized as an escape sequence by printf
and interpreted as an character with octal value of N
, thus the outer printf
prints this character, e. g. A
(because octal value of A
character in ASCII is 101
)
- the outer
$()
then ensures that the resulting character is not printed, but substituted into shell command
- after the final substitution, the command looks like this:
char=A
The reason why 2 conversions are needed for obtaining a character is obvious:
- character values are generated by
seq
in decimal representation
printf
does not accept numbers in decimal representation, instead octal representation has to be used
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…