Is there any way to check the first $NUM chars of a variable?

For example if I have a veriable which contains

VAR="LONGMORE LTD"

Can i check the first character begins with L?

Recommended Answers

All 2 Replies

With ksh93, bash, zsh:

[[ $VAR == L* ]]&&echo OK||echo KO

For old shells use case:

case $VAR in L*) echo OK;;*)echo KO;;esac

Hey there,

If you need to be more generic (like sometimes you want to match the first 3 letters and sometimes 2, etc) you can use expr's : (colon) operator to do string and substring matching.

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.