Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


bash string check

bash string check


why test returns "clean" what is marked in red in belongs strings comparison?

CODE -->

# d=274y82heE
# [[ $d =~ [^[:alnum:]] ]] && echo "not clean $d" || echo "clean $d"
clean 274y82heE
# d=274y82he^E
# [[ $d =~ [^[:alnum:]] ]] && echo "not clean $d" || echo "clean $d"
not clean 274y82he^E

# d=/one/ssfdsf/274y82he^E/end/end
# [[ $d =~ /one/ssfdsf/[^[:alnum:]]/end/end ]] && echo "not clean $d" || echo "clean $d"
clean /one/ssfdsf/274y82he^E/end/end
# [[ $d =~ /one/ssfdsf/[[:alnum:]]/end/end ]] && echo "not clean $d" || echo "clean $d"
clean /one/ssfdsf/274y82he^E/end/end
# [[ $d =~ ^/one/ssfdsf/[[:alnum:]]/end/end ]] && echo "not clean $d" || echo "clean $d"
clean /one/ssfdsf/274y82he^E/end/end
# [[ $d =~ ^/one/ssfdsf/[[:alnum:]]/end/end$ ]] && echo "not clean $d" || echo "clean $d"
clean /one/ssfdsf/274y82he^E/end/end 

RE: bash string check


  • [[:alnum:]] - one alphanumeric character
  • [^[:alnum:]] - one non-alphanumeric character
In that place you have 8 alphanumeric character, then 1 non-alphanumeric character, then 1 alphanumeric character.

Not sure what is your goal, but try something like this :

CODE --> Bash

[[ $d =~ /one/ssfdsf/.*[^[:alnum:]].*/end/end ]] 


RE: bash string check


ok. but it doesn't return clean when I expect it clean:

CODE -->

# d=/one/ssfdsf/274y82heE/end/end
# [[ $d =~ ^/one/ssfdsf/[[:alnum:]].*/end/end$ ]] && echo "not clean $d" || echo "clean $d"
not clean /one/ssfdsf/274y82heE/end/end 

the goal is to check if there is "alnum" between ^/one/ssfdsf/ and /end/end$

RE: bash string check


Your current regular expression says :
  • starts with "/one/ssfdsf/"
  • then 1 alphanumeric character
  • then any number of any character
  • ends with "/end/end"
As your current string looks exactly like that, so the 1st echo gets executed, saying "not clean".

Quote (w5000)

the goal is to check if there is "alnum" between ^/one/ssfdsf/ and /end/end$
Your current regular expression says in that place has to be 1 alnum followed by anything or nothing.

I think you want this, so in that place to have 1 or more alphanumeric characters :

CODE --> Bash

[[ $d =~ ^/one/ssfdsf/[[:alnum:]]+/end/end$ ]] && echo "clean $d" || echo "not clean $d" 


RE: bash string check

indeed, I was pretty sure I had tested [[:alnum:]]+ in one of my first tries... I must had done some mistake

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close