Skip to content

Instantly share code, notes, and snippets.

@akhan4u
Created February 9, 2023 14:57
Show Gist options
  • Save akhan4u/ea7bf76997bfb82d6f05a0bcf5c32d25 to your computer and use it in GitHub Desktop.
Save akhan4u/ea7bf76997bfb82d6f05a0bcf5c32d25 to your computer and use it in GitHub Desktop.
Expr Syntax and bash equivalent
EXPR Syntax Bash Equiv Meaning
SEE NOTE 1 "${ARG1:-$ARG2}" ARG1 if it is neither null nor 0, otherwise ARG2
ARG1 & ARG2 $(( ARG1 & ARG2 )) ARG1 if neither argument is null or 0, otherwise 0
ARG1 < ARG2 $(( ARG1 < ARG2 )) ARG1 is less than ARG2
ARG1 <= ARG2 $(( ARG1 <= ARG2 )) ARG1 is less than or equal to ARG2
ARG1 = ARG2 $(( ARG1 == ARG2 )) ARG1 is equal to ARG2
ARG1 != ARG2 $(( ARG1 != ARG2 )) ARG1 is unequal to ARG2
ARG1 >= ARG2 $(( ARG1 >= ARG2 )) ARG1 is greater than or equal to ARG2
ARG1 > ARG2 $(( ARG1 > ARG2 )) ARG1 is greater than ARG2
ARG1 + ARG2 $(( ARG1 + ARG2 )) arithmetic sum of ARG1 and ARG2
ARG1 - ARG2 $(( ARG1 - ARG2 )) arithmetic difference of ARG1 and ARG2
ARG1 * ARG2 $(( ARG1 * ARG2 )) arithmetic product of ARG1 and ARG2
ARG1 / ARG2 $(( ARG1 / ARG2 )) arithmetic quotient of ARG1 divided by ARG2
ARG1 % ARG2 $(( ARG1 % ARG2 )) arithmetic remainder of ARG1 divided by ARG2
STRING : REGEXP [[ $STRING =~ $REGEX ]] anchored pattern match of REGEXP in STRING
match STRING REGEXP [[ $STRING =~ $REGEX ]] same as STRING : REGEXP
substr STRING POS LENGTH ${STRING:$POS:$LEN} substring of STRING, POS counted from 1 (Note bash pos counts from 0)
index STRING CHARS No simple equiv, but the string can be slpit and the remainder counted. index in STRING where any CHARS is found, or 0
length STRING "${#STRING}" length of STRING
#NAME? Not needed interpret TOKEN as a string, even if it is a keyword like 'match' or an operator like '/'
( EXPRESSION ) ( EXPRESSION ) value of EXPRESSION

NOTE1 :- ARG1 | ARG2 is too hard to embed in a markdown table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment