remove


Git: Remove or Delete file from Git repository

To remove a file from the Git repository but not delete it from your filesystem, execute the following:

git rm --cached file_to_keep.txt;
git commit -m "Removing file_to_keep.txt from Repository";

To remove a file from the Git repository and the filesystem, use the following:

git rm file_to_delete.txt;
git commit -m "Removing and Deleting file_to_delete.txt";

Bash: Remove the last character from each line 1

The following script, uses rev and cut to remove the last character from each line in a pipe.
rev utility reverses lines character-wise.
cut removes sections  from each of line.
It is a very simple script where we reverse the line once, remove the first character (which was the last one in the original form of the line) and finally we reverse the line back with the last character missing.


echo -e "hi\nHI" | rev | cut -c 2- | rev;

# Will produce:
h
H

 


Remove the first character from a bash variable

The following method, removes the first character from a bash variable by using a regular expression that matches any character.


VAR=${VAR#?};

The functionality ${string#substring} deletes shortest match of $substring from the front of $string.
The question mark ? matches zero or one of the previous regular expression. It is generally used for matching single characters.
So by using ? as our $substring we instruct bash to match any character at the beginning of the variable and remove it.

Example


$ VAR="Banana";
$ VAR=${VAR#?};
$ echo $VAR;
anana