How to Remove Last Character from String in Bash (With Examples)


Often you may want to use Bash to remove the last character from a string.

There are two common ways to do so:

Method 1: Remove Last Character from String in Bash Using ?

echo ${my_variable%?}

This particular example will remove the last character from the variable named my_variable.

Feel free to use as many ? characters as you would like to remove the last n characters from a variable.

Method 2: Remove Last Character from String in Bash Using ::

echo "${my_variable::-1}"

This particular example will remove the last character from the variable named my_variable.

Note that this syntax only works in Bash 4 or higher versions.

Also note that you can change the -1 to a different negative value to instead remove the last n characters from a variable.

The following examples show how to use each of these methods in practice.

Example 1: Remove Last Character from String in Bash Using ?

Suppose we create a variable in Bash named my_variable that contains the following string:

  • “this is a really great string”

Suppose that we would only like to remove the last character from the string.

We can use the following syntax to do so:

my_variable="this is a really great string"
echo ${my_variable%?}

The following screenshot shows how to use this syntax in practice:

Bash remove last character from string

Notice that this returns the entire string with the last character removed.

We can also use more ? characters to remove additional characters from the end of the string.

For example, we can use the following syntax to remove the last 3 characters from the string:

my_variable="this is a really great string"
echo ${my_variable%???}

The following screenshot shows how to use this syntax in practice:

Bash remove last n characters from string

Notice that this returns the entire string with the last 3 characters removed.

Example 2: Remove Last Character from String in Bash Using ::

Another way to remove the last character from a string in Bash is by using the :: syntax.

We can use the following syntax to do so:

my_variable="this is a really great string"
echo "${my_variable::-1}"

The following screenshot shows how to use this syntax in practice:

Notice that this returns the entire string with the last character removed.

We can also use a different number besides -1 to remove additional characters from the end of the string.

For example, we can use the following syntax to remove the last 3 characters from the string:

my_variable="this is a really great string"
echo "${my_variable::-3}"

The following screenshot shows how to use this syntax in practice:

This returns the entire string with the last 3 characters removed.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Check if String Starts with Specific Value
Bash: How to Count Number of Words in String
Bash: How to Count Number of Characters in String

Leave a Reply