7

I need to calculate the length of a string using pure sh shell only. What is happening is that /bin/sh is actually a soft link to bash or another shell. Hence ${#STRING} gives the length of string as it is advance bash feature.

Can someone tell me how I can find length of string? I am using Solaris 5.10 Sparc architecture

  • It is not an advanced bash feature. The ${#STRING} parameter expension is defined by posix. And it also works under dash which is a POSIX compliant shell without bash-only features. – josch Jul 24 at 17:11
10

wc -m counts the chars in a string. So you can do something like:

STRLENGTH=$(echo -n $STRING | wc -m)

Alternative syntax:

STRLENGTH=`echo -n $STRING | wc -m`

The -n flag for echo stops it from printing a newline. The flag might be different on Solaris 5. Check man echo

  • Finally something that might work. Except it won't, because it will add trailing newline, which will be counted by wc. And there is no portable way to suppress it! Some shells use -n parameter, some use some escape sequence and neither is specified in POSIX. – Jan Hudec Sep 5 '14 at 6:16
  • It does not has to be portable. It must only work under Solaris 5.10. But you are right. I will edit my answer accordingly – Peter Lamby Sep 5 '14 at 6:18
  • i should then do LEN-1 to get original length right? – Niraj Sep 5 '14 at 6:18
  • can you suggest a way that will work on any linux or unix platform? – Niraj Sep 5 '14 at 6:21
  • 3
    @Niraj: Use printf instead of echo. It should exist everywhere too and works everywhere the same. – Jan Hudec Sep 5 '14 at 6:23
8

Here are couple of ways to do it.

myvar="This is a test"
echo "${#myvar}"
14

Or

expr length "${myvar}"
14
2

Here is couple of ways :

echo ${#VAR}
echo -n $VAR | wc -m
echo -n $VAR | wc -c
printf $VAR | wc -m
expr length $VAR
expr $VAR : '.*'

http://techopsbook.blogspot.in/2017/09/how-to-find-length-of-string-variable.html

  • (1) Six is not a “couple”.  (2) These are not equivalent.  Please explain the differences. – G-Man Oct 5 '17 at 19:17
  • These are couple if you count equivalent as single. – Mukesh Shakya Oct 6 '17 at 20:19
  • 1) echo ${#VAR} 2)echo -n $VAR | wc -m, echo -n $VAR | wc -c, printf $VAR | wc -m 3)expr length $VAR ,expr $VAR : '.*' – Mukesh Shakya Oct 6 '17 at 20:20
  • There's a lot of variation in how "couple" is used. No need to shame users for their language use, especially in cases where native speakers will have differing views of what's acceptable. Anyway, off topic - leave that to ell.stackexchange.com – henry Mar 10 '18 at 19:24
2

Using ${#string} to get the length of $string is a POSIX shell parameter expansion. It is not a bash-only feature.

On Solaris 5.10, if /bin/sh or /usr/bin/sh (as mentioned in the sh(1) manual) does not support this, then /usr/xpg4/bin/sh will.

To get POSIX behaviour on a Solaris 5.10 system, your PATH should be set to

/usr/xpg6/bin:/usr/xpg4/bin:/usr/ccs/bin:/usr/bin

(in that order), as described in the standards(5) manual.

1

I don't think "pure sh" can do it. But you don't need to do anything in "pure sh"; you need to do it in sh plus standard utilities. The most straightforward way is:

strlength=`expr "$string" : ".*"`

(expr is POSIX). Or if you don't have expr (Solaris 5.1 is ancient), you could also use wc together with printf like:

strlength=`printf "%s" "$string" | wc -m`

Just don't try to use echo, because echo will add trailing newlines and there is no standard way to suppress it.

Note that wc -m counts characters and wc -c counts bytes if there is a difference in current locale.

  • it will not work under sparc arcitecture – Niraj Sep 5 '14 at 6:21
  • @Niraj: What won't work under Solaris? They don't have expr? Do they have printf and wc? – Jan Hudec Sep 5 '14 at 6:22
  • I think -c counts the bytes of the string. This will probably not give you the correct value if you are using a multibyte characterset. – Peter Lamby Sep 5 '14 at 6:23
  • @PeterLamby: You are right. -m is probably better here. – Jan Hudec Sep 5 '14 at 6:30
  • @Niraj: Adjusted the process substitution to backticks; (pre-POSIX) bourne shell didn't have $() process substitution. – Jan Hudec Sep 5 '14 at 6:50
0

(Though there are already many answers, but feels they are not so intuitive, so I would add one to make it clear on first sight)

If you just want to do it in shell (bash) command line (or even script).

You can use either of following ways:

  • Use echo -n and wc -m
  • Use printf and wc -m

e.g

# 5
echo -n hello | wc -m

# 11
echo -n "hello world" | wc -m

# 5
printf "hello" | wc -m

# 11
printf "hello world" | wc -m

# 11
printf "hello %s" world | wc -m

Tips:

  • echo by default will append a new line at end, so -n option is needed to avoid that.
  • If there are space among input string, then should quote it.
-1

set var="test test test";

echo $%var;

14

-2

You could do this fairly simply in python

>>> len('hello world')
11
  • In theory, you could run python as/in a shell. That said, it looks like their string is a environmental variable and a more complete answer would take that into account. Also, whether such an old version of solaris would have python - notice Kusalanada's answer which goes into the... oddities of that particular flavour of unix – Journeyman Geek Jul 25 '18 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.