-1

I am currently writing a program in the raspberry pi that will allow a user to enter a string, and have the program count the amount of letters in it. I am looking into expr but I don't know how to use it, and what this actually does. Could you let me know what this means and how I can use it for my program.

This is what I currently have.

#!/bin/bash
echo "Enter a word."
read str
"help here please!"

Thanks

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Joe Dale
  • 71
  • 6
  • 1
    Maybe you should ask the teacher if you're struggling with the homework! – Swifty124 Oct 16 '14 at 08:24
  • 2
    You don't need to use `expr` since this will fork another process thus occupy more resources. It is as simple as `echo "${#str}"` – Valentin Bajrami Oct 16 '14 at 08:24
  • Do you want to count characters or bytes? Some languages have multibyte characters. – Mark Plotnick Oct 16 '14 at 10:22
  • I would like to count the characters in the string entered by the user. – Joe Dale Oct 16 '14 at 11:55
  • Related: [Having a problem with a shell script counting characters](http://unix.stackexchange.com/q/156229/80216), [Number of characters in a shell command's output](http://unix.stackexchange.com/q/160497/80216), and [Trying to find a better way to count letters in a file or shell variable](http://unix.stackexchange.com/q/156571/80216). – G-Man Says 'Reinstate Monica' Oct 16 '14 at 18:00

1 Answers1

1

You could use this

expr length $str

Hopefully this should help!

Jas
  • 346
  • 1
  • 2
  • 11
  • This, I have found, is a very longwinded answer compared to what val0x00ff has suggested. I much prefer to use what he has said as it makes things a lot easier. Thanks – Joe Dale Oct 16 '14 at 08:34
  • OK maybe next time, you decide whether you want to use 'expr' or not. Make it a bit clearer – Jas Oct 16 '14 at 08:36