# 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.
# -------------------------------------------------------------------------
if [ $# -eq 0 ]
	echo "$0 string1 string2"
	exit 1
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 via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs 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

  • Goce Apr 13, 2009 @ 11:52

    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 “

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

      concatenation is performed
      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.

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