Length of the string is the number of characters in the strings content. There are many different ways to compute the length of the string. Here we will see some of the ways to find the length of the string using the shell script.
> cat length.sh
#!/bin/bash
str="Unix"
#1. using the wc command
len=`echo $str|wc -c`
echo "length of $str=$len"
#2. using the awk command
len=`echo $str|awk '{print length}'`
#3. using the expr command
len=`expr length "$str"`
echo "length of $str=$len"
#4. using the shell built in hash(#)
echo "length of $str=${#str}"
> cat length.sh
#!/bin/bash
str="Unix"
#1. using the wc command
len=`echo $str|wc -c`
echo "length of $str=$len"
#2. using the awk command
len=`echo $str|awk '{print length}'`
#3. using the expr command
len=`expr length "$str"`
echo "length of $str=$len"
#4. using the shell built in hash(#)
echo "length of $str=${#str}"
No comments:
Post a Comment