Shell Script – Problem with Counting Characters

shell-script

I'm trying to learn the basics and I have run into an issue with my script counting the characters of a user's input. Here is my script, can someone point out where I'm going wrong please?

#!/bin/bash

echo "Enter a word!"    
read INPUT_STRING   
len= echo $INPUT_STRING | wc -c 
echo "Your character length is " $len
exit

Best Answer

every beginning is hard:

#!/bin/bash
read INPUT
echo $INPUT
len=$(echo -n "$INPUT" | LC_ALL=C.UTF-8 wc -m)
echo $len

specifically, there must not be a space surrounding = and a separate command needs to be enclosed inside $(...). Also, you might want to write your variables in quotes " using this syntax "${INPUT}", this ensures that the variable is not accidentally concatenated with what follows and can contain special chars (e.g. newlines \n).

Related Question