![Forrest logo](/images/logo.png)
expr:tldr:30178
expr: Get the first char position from a specific set in a string.
$ expr index "${string}" "${chars}"
try on your machine
The command expr index "${string}" "${chars}"
is used to find the index (position) of the first occurrence of any character in the chars
string within the string
string. Here's how it works:
${string}
is a variable representing the string in which you want to search for a character.${chars}
is a variable representing the characters you are searching for within thestring
.- The
expr index
command is used to search for the first occurrence of any character from${chars}
within${string}
. - If a character from
${chars}
is found in${string}
, the command returns the index (position) of the first occurrence. The first character in${string}
has an index of 1. - If none of the characters from
${chars}
are found within${string}
, the command returns 0. - The result of this command is often used in shell scripts or command line operations to determine the index of a specific character within a string.
For example, if you have string="Hello, World!"
and chars="Wo"
, the expr index "${string}" "${chars}"
command will return 8 because the first occurrence of either 'W' or 'o' in the string is at index 8.
This explanation was created by an AI. In most cases those are correct. But please always be careful and
never run a command you are not sure if it is safe.