tr Command in Linux With Examples

In Linux, the “tr” command short form of “Translate”, and is utilized for translating as well as deleting characters from user input and print output after modification. Additionally, you can perform various transformations through the “tr” command, such as text replacement, case conversion, squeezing characters, and many more. The objective of this article is to explain the “tr” command with various examples in Linux. The content that carried out this guideline is given below:

Let’s explore the “tr” command.

How Does the “tr” Command Work in Linux?

The working of the “tr” command is quite simple and accepts two sets of characters. This command translates the characters in the first set from the character placed in the second set. The syntax of the “tr” command is as below:

Syntax:

$ tr [options] [SET1] [SET2]

In the above syntax, the “SET1” specifies the characters on which transformation is applied from the string placed in “SET2”. To carry out this functionality, various options are supported by the “tr” command described below:

OptionsDescription
-d (delete)This option deletes characters from SET1 input.
-t (truncate)It truncates SET1 to SET 2 length.
– versionThis option displays the version information.
-c (complement)This option specifies the complement of SET1.
-s (squeeze)It squeezes the repeated character placed in SET1 or SET2.
How to install Kali Linux in Window...

To explore the extended features of the “tr” command, you can utilize the “tr – help” utility as follows:

$ tr --help

Let’s explore the practical usage of the “tr” command.

How to Use tr Command in Linux?

The “tr” command can replace, delete or convert characters from lowercase to uppercase or vice versa. Different practical usage of the “tr” command is explained below:

Example 1: How to Replace Specific Characters?

An example is considered to replace specific characters from the input characters. For instance, the “echo” is piped with the “tr” command in the below example. The “tr” command will replace the characters “nux” with “one”:

$ echo 'Linux' | tr 'nux' 'one' 

The output returns the “Lione” from the input “Linux” by replacing the characters “nux” with the “one”.

Example 2: How to Replace Character Range?

The “tr” command provides a feature to replace one or more characters provided by the range. In the following example, the “tr” command will replace the range of characters “a-f” with “x-z”:

$ echo 'among' | tr 'a-f' 'x-z' 

The output returns the “xmong” from “among” after replacing characters of range “a-m” from “x-z”.

Example 3: How to Ignore Specific Characters?

To ignore the specific character, the “tr” command can be used with the “-c” option that replaces all characters (which are not available in the “Set1”) with the character of the “Set2”:

$ echo 'linux' | tr -c 'l' 'y' 

The output displays that all characters are replaced with the character of the second set except “l”.

Example 4: How to Delete Specific Characters?

An example is carried out to delete the specific character through the “-d” option with the “tr” command. The characters specified after the “-d” option will be deleted:

$ echo 'Ubuntu' | tr -d 'Ub'

After executing the above command, you can verify that “Ub” has been successfully deleted from the input string “Ubuntu”.

Example 5: How to Remove Extra Spaces?

To remove the repeated space characters, the “-s” option is utilized with the “tr” command as below. The space is defined in the quotes(‘’), which will remove the extra spaces between the “Linux”, “\”, and “Ubuntu”: 

$ echo 'Linux   \   Ubuntu' | tr -s '  '

In the above display, the empty repeated spaces have been successfully removed.

Example 6: How to Change LowerCase to UpperCase?

Let’s carry out an example of the conversion from lowercase to uppercase. To do this, the syntax is provided below:

$ echo "linux is the best command prompt. " | tr "a-z" "A-Z"

From the above figure, you can verify that lower case alphabets have been successfully converted to upper case.

Example 7: How to Remove Non-Numeric Characters?

To remove the non-numeric characters from the string, specify the expression [:digit:] as below:

$ echo ‘your pin is 200382’ | tr -cd ‘[:digit:]’

The output shows that all characters have been successfully removed, excluding the “200382” digits.

Example 8: How to Remove Numeric Characters?

For removing the numeric characters from the string, the “-d” option is utilized with the expression [:digit:] as below:

$ echo ‘your pin is 200382’ | tr -d [:digit:]

The output returns all characters except the “200382” digits.

How to Use the tr Command With Bash Scripts?

An example is considered to use the tr command with the existing bash “hello.sh” file. For instance, the “[:print:]” expression is utilized to print to bash script as below:

$ tr -cd [:print:]< hello.sh

The “cat” command is piped with the “tr” in the following screenshot. The “tr” command will delete the character “E” from the script as below:

$ cat hello.sh | tr -d E

The output “VRYTHING GOOD” shows that the “E” character has been successfully removed from “EVERYTHING GOOD”.

Note: All of the above-stated “tr” options can be utilized in the bash scripts.

That is all from this guide.

Conclusion

The “tr” command is utilized to transform strings from standard input and display the output after performing operations. These operations include converting lower case to upper case, excluding the non-numeric number, deleting or replacing characters, and many more. This article has explained the “tr” command and practical examples in Linux systems. The “tr” command is also used in the bash scripts, which we have covered in this guide.