Remove Last character from String in Linux

29 Jun, 2021
In this article, we will discuss how to remove the last character from the string in Linux. 
In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different commands by which you can remove the last character of a string in Linux. 
Method 1: Using the cut command
The cut command is used to extract some specific section from the string or from a file and prints the result to standard output. You can also use this command for removing the characters from a string. There are two ways to remove the last character from the string using the cut command. These are as follows:
When you know the length of a string
Syntax:
cut <character> <range> 
Example:
$ echo "linux" | cut -c 1-4
This method works when you know the length of the given string. It first takes the string and pipes it with the cut command. You have to provide a range from the start of the string to the 2nd last character of the string. It extracts the characters from that range, leaving the last character, and prints the result to standard output.
Output: 
Another way is to use the complement option of the cut command.
Syntax:
cut --complement <complement pattern>
Example:
$ echo "linux" | cut --complement -c 5
In this method, you have to use the complement option present in the cut command. The complement command can work with bytes, characters, and fields. Here we are using character -c. So here we have provided (length of string)-1 after the -c command, which will cut the last character and print the result, leaving the last character of the string.
Output:
When you don’t know the length of the string
Syntax:
$ echo "linux" | rev | cut -c2- | rev
In this method, you have to use the rev command. The rev command is used to reverse the line of string characterwise. Here, the rev command will reverse the string, and then the -c option will remove the first character. After this, the rev command will reverse the string again and you will get your output.
Output:
Method 2: Using the sed command
The sed command stands for stream editor. It is a powerful tool that is used for manipulating and editing streams of text. It also supports regular expressions which can be used for pattern matching. You can also use the sed command to remove the characters from the strings.
$ echo "linux" | sed 's/.$//'
In this method, the string is piped with the sed command and the regular expression is used to remove the last character where the (.) will match the single character and the $ matches any character present at the end of the string.
Output:
Method 3: Using awk 
The awk is a scripting language that is used for pattern matching and text processing. Mostly it is used as a reporting and analyzing tool. So now you will see how we can use awk to remove the last character from the given string.
$ echo "linux" | awk '{ print substr( $0, 1, length($0)-1 ) }'
This method works as our given string pipes with the awk and then in awk, the string is processed. Here the length($0)-1 means removing the last character by deducting ‘1’ from the string’s total length. Through this process, the command will print the string from the 1st character up to the 2nd character. 
Output:
Linux-text-processing-commands
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.