Linux: get the length of a string

To get the length of a string on a Linux-Shell as number of characters you have various possibilities:

bash> VAR="linux-operating-system"

bash> expr length $VAR
22

bash> echo ${#VAR}
22

bash> echo $VAR | awk '{print length}'
22

bash>

To store the length in a variable you could do this:

bash> LEN=`expr length $VAR`

bash> echo $LEN
22
bash>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: