Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a character from the end of a variable

Tags:

bash

shell

People also ask

How do I remove a character from a variable in Python?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

How do I remove a character from a variable in R?

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.

How do I remove the last 3 letters from a string?

Use the String. slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.


Use

target=${1%/}

A reference.


Use target=${1%/}

See this the parameter substitution of this bash scripting guide for more.


I think better solution to canonize paths is realpath $path or with -m option if it doesn't exist. This solution automaticaly removes unnecessary slashes and adds pwd


Be careful, bash3 added perl-similar regex to bash. The guide mentioned covers this as well as the official guide at GNU , but not all references do.

What did I do?

Substitute 2.19/* to be 2.19.

Solution

VER="2.19/foo-bar"
NEWVER=${VER%/*}