Shell Script To Concatenate Two String Given as Input including its length

in Categories Academic, Decision Making, String Management last updated October 20, 2008
# Write a shell program to concatenate to two String given as input and display the 
# resultant string along with its string length.
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions :
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
if [ $# -eq 0 ]
	echo "$0 string1 string2"
	exit 1
len=$(expr length $OUT)
echo "Concatenate string is - $OUT"
echo "String Length is - $len"

Share this on:

5 comment

  1. get rid of the ` in your expr

    for instance, if you wanted to get the length of s3 in len, you;d do

    len= expr length $s3
    echo $len

  2. Why doesn’t this work?????

    echo “Enter first string:”
    read s1
    echo “Enter second string:”
    read s2
    len=`echo $s3 | wc -c`
    len=`expr $len – 1`
    echo “Concatenated string is $s3 of length $len “

  3. what is length in the expression “len=$(expr length $OUT)”
    did you write that function or SHELL has that function?

    Have a question? Post it on our forum!