Sponsored By

Sign in to follow this  
shanenin

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

Share this post


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.

Share this post


Link to post
Share on other sites

thanks for the explanation.

it turns out my script was pretty meaningless. I could have just done

[[ ${VAR} == *[^a-bA-B1-9]* ]]

or

[[ ${VAR} == *[^[:alnum:]]* ]]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this