Sunday, 28 June 2020

In Bash, how can I check if a string begins with some value?

This snippet on the Advanced Bash Scripting Guide says:

# The == comparison operator behaves differently within a double-brackets
# test than within single brackets.

[[ $a == z* ]]   # True if $a starts with a "z" (wildcard matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

string='My long string'
if [[ $string == *"My long"* ]]; then
  echo "It's there!"
fi

No comments:

Post a Comment