
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
ASCII codes are widely used for standard character encoding that controls and represents text in computers.
In this tutorial, we’ll discuss how to get ASCII values for characters using a Bash script.
First, we’ll discuss the printf command. Next, we’ll explore the od command to encode characters using an octal dump. Lastly, we’ll talk about various ASCII conversion techniques to calculate the value of a character.
To begin with, we can use the printf command to obtain an ASCII code for a character.
For instance, to get the value of the letter a, we’ll add a format specifier to printf:
$ cat printfValue.sh
read -p "Enter a character: " input_char
ascii_value=$(printf "%d" "'$input_char")
echo "ASCII value of $input_char: $ascii_value"
Now, we’ll make the file executable using the chmod command, and run the script:
$ chmod +x printfValue.sh
$ ./printfValue.sh
Enter a character: a
ASCII value of a: 97
To elaborate, the above script uses the read command to prompt us to input an ASCII character. After entering the character, the %d format specifier tells printf to format the value as a decimal number.
Thus, the command displays an ASCII value of the given character using the echo command.
Another approach to get the ASCII value of a given character is to use the od command. The od command stands for octal dump.
Notably, it’s used for various formats that include octal, hexadecimal, and ASCII. It also provides a flexible way to view the data within files.
Let’s view the odValue.sh script:
$ cat odValue.sh
read -p "Enter a letter: " letter
ascii_value=$(od -An -t u1 <<< "$letter")
echo "The ASCII value of '$letter' is $ascii_value"
Now, we’ll make the script executable and check its output:
$ chmod +x odValue.sh
$ ./odValue.sh
Enter a letter: a
The ASCII value of 'a' is 97
In the above code, we calculate the ASCII value of a character represented by the variable $letter using a here-string and the od command.
To elaborate, the here-string feeds the value of the $letter variable to the standard input of the od command. The od command processes the character and gets its octal representation.
In particular, -An -t u1 are the options for the od command:
In this example, we’ve set the format to u1, which indicates that the data should be displayed as unsigned decimal numbers using a format of 1 byte per unit.
After that, the result is stored in ascii_value, and printed using echo.
In the following subsections, we explore various approaches to obtain the ASCII value of characters in more general Bash scripting tasks.
We can use the tr command with printf to get the ASCII value of a given character while working with only one text case.
The tr command stands for translate, which we can use to perform simple character-level transformations.
Now, let’s get the ASCII value of the character a:
$ cat trValue.sh
read -p "Enter a letter: " letter
uppercase_letter=$(echo "$letter" | tr 'a-z' 'A-Z')
ascii_value=$(printf "%d" "'$uppercase_letter")
echo "The ASCII value of uppercase '$uppercase_letter' is $ascii_value"
In this example, the script prompts the user to input the character. Subsequently, the tr command translates the characters from lowercase (a-z) to uppercase (A-Z).
After that, the format specified in printf converts the character according to its ASCII value. Lastly, we use echo to print the value to the terminal.
Let’s make the script executable and run it:
$ chmod +x trValue.sh
$ ./trValue.sh
Enter a letter: b
The ASCII value of uppercase 'B' is 66
After executing the example, we can see that the script converted the value into uppercase, and displayed the ASCII result.
An alternative method to get the ASCII value for a number of character is to use a for loop with printf.
So, let’s view the forloopValue.sh script:
$ cat forloopValue.sh
#!/bin/bash
read -p "Enter a string of characters: " input_string
echo "ASCII values of characters in the input string:"
for ((i = 0; i < ${#input_string}; i++)); do
character="${input_string:i:1}"
ascii_value=$(printf "%d" "'$character")
echo "$character: $ascii_value"
done
Now, let’s execute and check the output of the script:
$ chmod +x forloopValue.sh
$ ./forloopValue.sh
Enter a string of characters: aBc
ASCII values of characters in the input string:
a: 97
B: 66
c: 99
In summary, the above code uses a for loop to iterate through a string of characters. We use ${input_string:i:1} to extract each character individually:
Subsequently, we use printf to get the ASCII value of the given characters. Lastly, the echo command displays the value.
Similarly, we can use the ord function to encode the value of a character.
The ord function is commonly used in programming languages. In practice, we use ord to obtain the integer representation of a character.
However, there is no such builtin in Bash, we’ll create one:
$ cat ordValue.sh
#!/bin/bash
ord() {
LC_CTYPE=C printf '%d' "'$1"
}
read -p "Enter a character: " character
result=$(ord "$character")
echo "ASCII value of '$character': $result"
Now, let’s get a character as an input, and use the ord function to calculate its value:
$ chmod +x ordValue.sh
$ ./ordValue.sh
Enter a character: a
ASCII value of a: 97
The above script takes a character from the user as an input. Next, the ord function gets the ASCII value of the given character:
Moreover, the format specifier %d specifies that printf should format the value as a decimal number. Notably, the single quotes around $1 ensure that printf treats it as a character and not as a string.
Lastly, the result variable stores the value. After that, the echo command displays the value of the character.
In this article, we learned how to get ASCII values for a character using a Bash script.
First, we discussed the printf function. After that, we explored the od command to calculate the ASCII value. Lastly, we demonstrated variations of ASCII conversion using tr, for, and the ord function.