Daily Archives: 10.03.2013


To print the first and last characters of a string in bash

The actual string: var="somebash" This outputs "some": echo ${var:0:4} echo $var | sed 's/\(.\{4\}\).*/\1/' echo $var | awk '{print substr($0,1,4)}' echo $var | cut -c1-4 printf "%.4s\n" "$var" echo "${var%${var#????}}" This outputs "bash": echo $var | cut -c4- echo "${var#????}" echo ${var:4} echo $var | awk '{print substr($0,5)}'