user2957523
user2957523

Reputation:

bash shell program does not show ascii value

I am trying to input a character from the user and display the corresponding ascii value to the screen. This is my code. If there are any errors please rectify and post it.

echo Enter a character
read n
printf "%d" $n; 

Error-: ./ascii.sh: line 3: printf: a: invalid number

Upvotes: 1

Views: 138

Answers (1)

pgl
pgl

Reputation: 8031

You need to put a single quote before $n:

echo Enter a character
read n
printf "%d" \'$n

You can also get rid of the echo:

read -p 'Enter a character: ' n
printf "%d" \'$n

Upvotes: 1

Related Questions