Pages

Thursday, October 31, 2019

Shell Scripting: Tidbits #1

Length of a String Stored in a Variable

${#VARIABLE}

eg.,
$ FRUIT="Orange"

$ echo ${#FRUIT}
6

In bash, variables that store values are known as parameters.

Ref: Bash Reference Manual -> Shell Parameter Expansion -> ${#parameter}

Remove First n Characters from a String

${VARIABLE:n}

eg.,
$ PARAM="Quick Brown Fox"

$ echo ${PARAM:6}
Brown Fox

Remove Last n Characters from a String

${VARIABLE::-n}

eg.,
$ PARAM="Quick Brown Fox"

$ echo ${PARAM::-6}
Quick Bro

Convert a String to Upper Case

${VARIABLE} | tr '[:lower:]' '[:upper:]' eg.,

$ NAME='Giri Mandalika'

$ echo ${NAME} | tr '[:lower:]' '[:upper:]'
GIRI MANDALIKA

Convert a String to Lower Case

${VARIABLE} | tr '[:upper:]' '[:lower:]' eg.,

$ NAME='Giri Mandalika'

$ echo ${NAME} | tr '[:upper:]' '[:lower:]'
giri mandalika

No comments:

Post a Comment