You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

bash: "expr index string1 string2" gives "syntax error"

In a bash script, I was trying to use the command

expr index string1 string2

to find the position of string2 in string1, but this does not work on OS X while it works ok on several Linux machines. Reading the man page, I realized expr does something quite different on OS X than on Linux.

Can anybody suggest which command I should use to get the same functionality of the above on OS X?

Thanks

Andreas

MacPro, Mac OS X (10.4.9), Server

Posted on Apr 11, 2007 11:45 AM

Reply
Question marked as Top-ranking reply

Posted on Apr 11, 2007 2:06 PM

The expr in Linux is generally GNU expr. Mac OS X uses BSD expr which does not have the index command. You could install/compile GNU expr or use this:

echo string1 | sed -n "s/[string2].*//p" | wc -c

Note the double quotes in the sed statement otherwise string2 will not be expanded.
11 replies

Apr 11, 2007 6:58 PM in response to LittleSaint

Hi LittleSaint,
That is so cool! and so ... UNIX. I would have pulled an Indiana Jones and whipped out awk. To steal your idea and translate it to zsh, that would look something like the following:

test=string1
echo ${#test%%[string2]*}

It doesn't seem to work in bash to do both of these operations in one expansion although bash does each operation. Thus, the same could be done in bash with an extra assignment but I can't believe there are still those who use bash. 🙂

That also seems to be a strange use of the word "index." I would have expected it to find the index of a substring, which seems to be pretty common usage in languages with which I'm familiar.
--
Gary
~~~~
A candidate is a person who gets money from the rich
and votes from the poor to protect them from each other.

Apr 11, 2007 9:33 PM in response to LittleSaint

Hi LittleSaint,
No, actually zsh starts numbering arrays at one, which does take some getting used to. From the zshparam manpage:
The elements are numbered beginning with 1, unless the KSH_ARRAYS option is set in which case they are numbered from zero.
However, that shouldn't affect the output of my command, even in bash, because it's testing the length of the string left after the substitution.

% test=aabbcc
% echo ${#test%%[cb]*}
2

For those using the "wrong" shell: 🙂

$ test=aabbcc
$ test=${test%%[bc]*}
$ echo ${#test}
2

--
Gary
~~~~
Q: Minnesotans ask, "Why aren't there more pharmacists
from Alabama?"
A: Easy. It's because they can't figure out how to get the
little bottles into the typewriter.

Apr 12, 2007 5:38 PM in response to Ken Nellis

Hi Ken,
Oh, OK. Then change my command to:

echo $((${#test%%[cb]*} + 1))

I guess I need expr. I see LittleSaint's point now; my index isn't the index in a zsh character array because they start at one. My number is correct for the bash index of a character in a character array. I thought that because bash array indices start at zero, "expr" would return a comparable answer but that's what I get for assuming.
--
Gary
~~~~
Lady Nancy Astor:
"Winston, if you were my husband, I'd put poison in your coffee."
Winston Churchill:
"Nancy, if you were my wife, I'd drink it."

Apr 13, 2007 8:59 PM in response to Ken Nellis

Hi Ken,
Wow! That's something you don't see every day; I'm really impressed. I had had a tough day too. Later I thought of a funny answer in which I blamed everything on the crazy shell I use. The difference between the two answers is just the flow of karma but stopping the flow of negative by saying something is a real and difficult choice and yours shows character. I'm sorry for going on too much but such things matter more than machines.

On the other hand, you're a royal pain in the ... just kidding. You're a strict task master but right; I read in the man page about it returning zero and forgot. However, mathematically it isn't too difficult; you just have to use modular arithmetic. In zsh, (I can learn) it would look like:

test=aabbcc
echo $(( ( ${#test%%[bc]*} + 1 )%( ${#test} + 1 ) ))

In bash, it would look like:

test=aabbcc
testtmp=${test%%[bc]*}
echo $(( ( ${#testtmp} + 1 )%( ${#test} + 1 ) ))

--
Gary
~~~~
In science it often happens that scientists say, 'You know that's a really good argument; my position is mistaken,' and then they actually change their minds and you never hear that old view from them again. They really do it. It doesn't happen as often as it should, because scientists are human and change is sometimes painful. But it happens every day. I cannot recall the last time something like that happened in politics or religion.
-- Carl Sagan, 1987 CSICOP keynote address

bash: "expr index string1 string2" gives "syntax error"

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.