Removing the first and last character of a string in Bash
In this post we will give you information about Removing the first and last character of a string in Bash. Hear we will give you detail about Removing the first and last character of a string in BashAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to remove the first and last character of a stringin Bash.
Consider we have the following string:
str="/google/"
Now, we want to remove the slashes / from both sides of a string.
Using the parameter expansion syntax
To remove the first and last character of a string, we can use the parameter expansion syntax ${str:1:-1} in the bash shell.
1 represents the second character index (included).
-1 represents the last character index (excluded).
It means slicing starts from index 1 and ends before index -1.
Here is an example:
str="/google/"modified="${str:1:-1}"echo $modified
Output:
"google"
Similarly, we can also use the following syntax:
str="/google/"str="${str#?}" # removes first characterstr="${str%?}" # removes last characterecho $str # "google"
Hope this code and post will helped you for implement Removing the first and last character of a string in Bash. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs