سؤال

Let's say I want to check if the first argument is 2 characters long. I tried

if [ ${#$1} -e 2]

but it doesn't work.

هل كانت مفيدة؟

المحلول

  • Don't use $ in the parameter expansion
  • Use -eq for numerical comparison
  • Add a space before the ]:

All in all:

if [ ${#1} -eq 2 ]
then
  echo "It's two characters long"
fi
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top