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

in Categories Academic, Decision Making, String Management last updated October 20, 2008
#!/bin/bash
# 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 : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
STR1=$1
STR1=$2
OUT="$1$2"
if [ $# -eq 0 ]
then
	echo "$0 string1 string2"
	exit 1
fi
 
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
    HTH
    ..

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

    #!/bin/sh
    echo “Enter first string:”
    read s1
    echo “Enter second string:”
    read s2
    s3=$s1$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!