How to Remove Last Character from Bash String [6 Methods]

A string is a collection of characters including numbers and special characters. In Bash scripting, string manipulation is one of the important tasks to accommodate necessary updates to information and data. To remove the last character from a string in Bash, follow the below method:

  1. Using the cut command: echo "$original_string" | cut -c -n (n=total number of characters excluding the last character)
  2. Using the sed command: echo "$original_string" | sed 's/.\{1\}$//'
  3. Using parameter expansion: echo ${original_string::-1}
  4. Using the awk command: echo "$original_string" | awk '{ print substr( $0, 1, length($0)-1 ) }'
  5. Using the ? symbol: echo ${original_string%?}
  6. Utilizing parameter expansion with string length: echo ${original_string:0: string_length -1}

1. Using the “cut” Command

The cut command extracts a range of a substring from a big string. With the echo command, it can remove characters from a string. Here is how:

#!/bin/bash
string="Welcome to Linuxsimply"
echo "$string" | cut -c -21
EXPLANATION

The output of the echo command goes as input to the cut command. Here, cut -c -21 specifies extracting the first 21 characters from the original string and printing it on the terminal.

The cut command has removed the last character from the string in bash.

2. Using the “sed” Command

The sed command can edit a string by removing or adding characters to that string. Piping the output of the echo command to the sed command, the last character can easily be removed. Here, I will show a bash script that will remove the last character from a string:

#!/bin/bash
string="Welcome to Linuxsimply"
echo "$string" | sed 's/.\{1\}$//'
EXPLANATION

The output of the echo command goes as input to sed 's/.\{1\}$/’. Here, sed 's/.\{1\}$//' is a command substitution for ‘sed’. The ‘s’ indicates that sed should perform a substitution. And .\{1\}$ matches the last character of the string. Finally, // replaces the last character with nothing.

The sed command has removed the last character.

3. Using Parameter Expansion

The parameter expansion is a method that can modify a string. The ${string_name::-1} removes the last character. Here is a Bash script on this:

#!/bin/bash
string="Welcome to Linuxsimply"
echo ${string::-1}
EXPLANATION

The echo ${string::-1} is a parameter expansion that specifies a substring up to the second last character.

Parameter expansion removed the last character.

4. Using “awk” Command

The awk command is a string processing tool that looks for a specific pattern inside a string and manipulates it. Use the substr( $0, start_character_position, original_string_length($0)-1 ) }' function inside the awk command with the print command to extract a substring excluding the last character from the original_string($0). Here, is a complete bash script to do this:

#!/bin/bash
string="Welcome to Linuxsimply"
echo "$string" | awk '{ print substr( $0, 1, length($0)-1 ) }'
EXPLANATION

The output of the echo command goes as input to the awk command. Here, substr( $0, 1, length($0)-1 ) extracts a substring starting from the first character and the length of the substring is of the original string length minus one.

The awk command has removed the last character from bash string.

5. Using “?” to Remove the Last Characters of a String

The “?” symbol removes the shortest matches from a string. ${original_string%?} fetches a substring from the original string excluding the last character from the string. Here is the Bash script how:

#!/bin/bash
string="Welcome to Linuxsimply"
echo ${string%?}
EXPLANATION

Here, ${string%?} removes the shortest possible match of the ‘?’ pattern from the ending of the string which is a single character.

The "?" symbol has removed the last character.

6. Using Bash Parameter to Remove the Last Character from a String

The last characters of a string can be removed by using the string length inside the echo command with a curly bracket. The syntax to remove the last character from a string is: echo ${original_string:start_index: length -(number of characters to remove from the end)}. However, it is a parameter expansion approach. Here is a complete Bash script for doing so:

#!/bin/bash
string="Welcome to Linuxsimply"
length=${#string}
echo ${string:0: length -1}
EXPLANATION

length=${#string} calculates the length of the string. After that, the ${string:0: length -1} represents a substring that starts from the first character of the original string and the length will be the original string length minus 1.

The last character has been removed.

Practice Problem

  1. Write a bash script to remove the first and last characters of a string.
  2. Write a bash script to remove the last 5 characters of a string.

Conclusion

This article explores different approaches to removing the last character from a string including cut command, sed command, awk command, and parameter expansion. This feature will equip a bash programmer to manipulate a string whenever necessary for efficient, accurate, and updated data management.

People Also Ask

How do I remove the last character of a string in Bash?

To remove the last character of a string in Bash, execute: echo ${original_string%?} command. It will remove the last character of a string in Bash and print the remaining part of the string in the terminal.

How do I remove the last space in Bash?

To remove the last space in a string in bash, execute nospace_string=${original_string% }. If the original string is ”Hello, how are you? ” then the nospace_string will be ”Hello, how are you? ”

How do you check if a string is empty in Bash?

To check if a string is empty in Bash, use the ‘-z’ option inside an if statement. If the string is empty, the output of the ‘-z’ option will be zero.

How to find substring from string in Bash?

To find a substring from a string in Bash, use the syntax: substring=${string:start_position:substring_length} This will extract a substring from the original string starting from the start_position of the original string, and the substring_length is the number of characters you want to extract.

Related Articles


<< Go Back to String Manipulation in Bash | Bash String | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment