97 words
1 minutes
How to remove the first/last characters from a variable in a shell script

How to remove the first/last characters from a variable in a shell script#

In shell scripting, you may need to remove the first or last characters from a variable. This guide provides methods to achieve this.

Removing the First Character#

To remove the first character from a variable, use the following syntax:

variable="example"
variable="${variable:1}"
echo $variable

Removing the Last Character#

To remove the last character from a variable, use the following syntax:

variable="example"
variable="${variable::-1}"
echo $variable

Conclusion#

By using these methods, you can easily remove the first or last characters from a variable in a shell script. This can be useful for various text processing tasks.


How to remove the first/last characters from a variable in a shell script
https://zxce3.net/posts/cli/how-to-remove-the-first-last-characters-from-a-variable-in-a-shell-script/
Author
Memet Zx
Published at
2022-07-18