Bash String Manipulation

This page answers questions like these:


Related Links:
Embed Quotes in a Shell Command or String
Shell String Containing Spaces is Not Splitting
Shell (Bash/Bourne/Korn) Alias Taking Multiple Arguments



First N Characters of a String:

$ STR="123456789" $ echo "${STR::3}" # Print the first 3 characters of string STR. 123

All but the First N Characters of a String:

$ STR="123456789" $ echo "${STR:3}" # Print all but the first 3 characters of string STR. 456789

Last N Characters of a String:

$ STR="123456789" $ echo "${STR: -3}" # Print the last 3 characters of string STR. 789

All but the Last N Characters of a String:

$ STR="123456789" $ echo "${STR::-3}" # Print all but the last 3 characters of string STR. 123456

Middle N Characters of a String:

$ STR="123456789" $ echo "${STR:2:3}" # Print 3 characters, starting from the character at index 2. 345


Related Links:
Embed Quotes in a Shell Command or String
Shell String Containing Spaces is Not Splitting
Shell (Bash/Bourne/Korn) Alias Taking Multiple Arguments

Home  >  Linux / Unix  >  Bash String Manipulation


Tags: first N chars of string, last N chars of string, all but the first N chars of a string, all but last N chars of a string, substring, string manipulation, shell, command, string, linux, unix, solaris, bsd, aix

Copyright © HelpDoco.com
shell-string-manipulation.txt
Linux-Unix/bash-string-manipulation.htm
5