Member Avatar for cristi2008

how can I limit the length of a variable??thanks

Hey There,

In bash (and similar shells) you can use read with the -n flag, like:

read -n4

if you want to limit the variable input to 4 characters.

If you want to just trim your variables, you can check the length of a variable by gettings the length value and running a simple if compare. so, if:

var=12345
echo $var = 12345
echo ${#var} = 5

if [ ${#var} -gt 4 ]
then
bail
fi

Hope that helps :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.