Bash check if file begins with a string

Bash code to check if file begins with a string

FILE=/etc/passwd
if [[ $FILE == /etc/* ]]; then
  echo "match"
fi
match
Env: GNU bash, version 4.3.11

Note the presence of double square brackets ([[ ]])

Bash code to check if file begins and ends with something

FILE=/etc/passwd
if [[ $FILE == /e*/passwd ]]; then
  echo "match"
fi
match
Env: GNU bash, version 4.3.11

Bash code for file name matching if filename has space

FILE="/etc/pass d"
if [[ $FILE == /*"/pass d" ]]; then
  echo "match"
fi
match
Env: GNU bash, version 4.3.11
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments