Bash Variable Question


Recommended Posts

I have been messing around with a bash script to test if characters are in a string

#!/bin/bash

read PASS
X=${#PASS}
Y=0
while (( Y < X )); do
   if [[ ${PASS:$Y:1} != [a-zA-Z1-9] ]]; then
       break
   fi
   let Y+=1
done
(( Y == X ))

notice this line in particular

while (( Y < X )); do

I called my variables without using the $ like this $Y and $X, and it still works. WHY?

it also works like this

while (( $Y < $X )); do

Edited by shanenin
Link to post
Share on other sites

It works because it works. The Bourne shell is the wrong to place to look if you're trying to find consistency.

The POSIX definition of arithmetic substitution includes only the $(()) form. I think it also requires dollar prefixes for variables, but I'm not sure. The alterate syntaxes (both the bare (()) and the 'let' form) were introduced in ksh and adopted by bash.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...