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

#!/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"
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • jdog Apr 24, 2009 @ 11:00

    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
    ..

  • Goce Apr 13, 2009 @ 11:52

    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 “

    • jyoti prakash ujjeniya Apr 20, 2010 @ 6:58

      concatenation is performed
      but
      length doesn’t calculated

  • Goce Apr 13, 2009 @ 11:49

    to get the length of a string can I use len=`echo $str | wc -c`?????

  • Goce Apr 13, 2009 @ 11:48

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.