How to remove (cut) last n characters from strings

Sometimes you need to cut characters from string. If you need substring starting from characters 4 it is simple:

[you@host ~]$ test="abcdefgh"
[you@host ~]$ echo $test | cut -c 4-
defgh

If you want to remove last 3 characters you can do this using:

[you@host ~]$ echo ${test:0:${#test}-3}
abcde

or

[you@host ~]$ echo $test | rev | cut -c 4- | rev
abcde

I prefer the second method.

Komentarze

Log in or create a user account to comment.

Ten kto umie pisze kod, ten kto nie umie pisze książki..., albo blog.

Pon Wt Śr Czw Pt So N
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
eZ Publish™ copyright © 1999-2025 eZ Systems AS