Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first character of a string in Bash

Tags:

bash

I need to calculate md5sum of one string (pathfile) per line in my ls dump, directory_listing_file:

./r/g4/f1.JPG ./r/g4/f2.JPG ./r/g4/f3.JPG ./r/g4/f4.JPG 

But that md5sum should be calculated without the initial dot. I've written a simple script:

while read line do     echo $line | exec 'md5sum' done  ./g.sh < directory_listnitg.txt 

How do I remove the first dot from each line?

like image 381
JosiP Avatar asked Jul 06 '11 09:07

JosiP


People also ask

How do I remove the first character of a string in Bash?

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 .

How do I remove a character from a string in Bash?

Remove Character from String Using trThe tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string.

How do you cut the first character in Linux?

Removing the first n characters To remove the first n characters of a string, we can use the parameter expansion syntax ${str: position} in the Bash shell.


2 Answers

myString="${myString:1}" 

Starting at character number 1 of myString (character 0 being the left-most character) return the remainder of the string. The "s allow for spaces in the string. For more information on that aspect look at $IFS.

like image 149
LiXCE Avatar answered Sep 22 '22 20:09

LiXCE


You can pipe it to

cut -c2- 

Which gives you

while read line do echo $line | cut -c2- | md5sum done  ./g.sh < directory_listnitg.txt 
like image 39
fulmicoton Avatar answered Sep 25 '22 20:09

fulmicoton