Remove first character of a string in Bash

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

remove first n characters from a line or string

method 1) using bash

 str="price: 9832.3"
 echo "${str:7}"

method 2) using cut

 str="price: 9832.3"
 cut -c8- <<< $str

method 3) using sed

 str="price: 9832.3"
 sed 's/^.\{7\}//' <<< $str

method 4) using awk

 str="price: 9832.3"
 awk '{gsub(/^.{7}/,"");}1' <<< $str

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.


There ia a very easy way to achieve this:

Suppose that we don't want the prefix "i-" from the variable

$ ROLE_TAG=role                                                                            
$ INSTANCE_ID=i-123456789

You just need to add '#'+[your_exclusion_pattern], e.g:

$ MYHOSTNAME="${ROLE_TAG}-${INSTANCE_ID#i-}"  
$ echo $MYHOSTNAME
role-123456789

Tags:

Bash