Bash Expr Substr Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn expr index syntax error more about Stack Overflow the company Business Learn more about hiring developers or posting bash expr index example ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Expr Syntax Error In Shell Script
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Syntax error Using expr substr in bash script up vote 0
Expr Syntax Error Multiplication
down vote favorite I have written small bash script to try to get the last x characters of a string. #!/bin/bash string="This is my string. I want THIS TEXT" echo -e "string: $string" length=${#string} echo "length: $length" start=$(($length-9)) echo "start: $start" text=`expr substr $string $start 9` echo "text: $text" exit 0 The output is giving me a "syntax error": string: This is my string. I want THIS TEXT expr mac length: 35 start: 26 expr: syntax error text: I'm sure it's fairly simple but I can't figure it out. Can anybody point out what I'm doing wrong? This is being run on SunOS. bash syntax-error substr share|improve this question asked Dec 9 '10 at 22:56 sdoca 3,116135196 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted how about this? echo ${string:(-4)} where -4 is your offset from the end share|improve this answer answered Dec 9 '10 at 23:12 damir 8442819 Thanks, that works and is much cleaner. –sdoca Dec 9 '10 at 23:17 +1 got there before me –SiegeX Dec 9 '10 at 23:18 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged bash syntax-error substr or ask your own question. asked 5 years ago viewed 3648 times active 5 years ago Related 2882Can a Bash scri
enter a title. You can not post a blank message. Please type your message and try again. This discussion is locked Andreas Haselbacher Level 1 (5 points) Q: bash: "expr index string1
Bash Substring
string2" gives "syntax error" In a bash script, I was trying to use the commandexpr index string1 string2to 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 http://stackoverflow.com/questions/4403953/syntax-error-using-expr-substr-in-bash-script use to get the same functionality of the above on OS X?ThanksAndreas MacPro, Mac OS X (10.4.9), Server Posted on Apr 11, 2007 11:45 AM I have this question too by LittleSaint,Solvedanswer LittleSaint Level 4 (2,900 points) A: 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 https://discussions.apple.com/thread/923299?tstart=0 or use this:echo string1 | sed -n "s/[string2].*//p" | wc -cNote the double quotes in the sed statement otherwise string2 will not be expanded. Posted on Apr 11, 2007 2:06 PM See the answer in context Close Q: bash: "expr index string1 string2" gives "syntax error" All replies Helpful answers by LittleSaint,Solvedanswer LittleSaint Apr 11, 2007 2:06 PM in response to Andreas Haselbacher Level 4 (2,900 points) Apr 11, 2007 2:06 PM in response to Andreas Haselbacher 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 -cNote the double quotes in the sed statement otherwise string2 will not be expanded. Helpful (0) Reply options Link to this post by Andreas Haselbacher, Andreas Haselbacher Apr 11, 2007 2:27 PM in response to LittleSaint Level 1 (5 points) Apr 11, 2007 2:27 PM in response to LittleSaint Thanks a lot - it does exactly what I need. Andreas Helpful (0) Reply options Link to this post by Gary Kerbaugh, Gary Kerbaugh Apr 11, 2007 6:58 PM i
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community https://ubuntuforums.org/showthread.php?t=834263 Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User Documentation Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Ubuntu Servers, Cloud and Juju Server Platforms [ubuntu] [SOLVED] syntax error 'expr' command problems Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to 5 of 5 Thread: [SOLVED] 'expr' command problems Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode expr syntax error June 19th, 2008 #1 Richard_ View Profile View Forum Posts Private Message Spilled the Beans Join Date Jun 2008 Location Durban Beans 13 DistroUbuntu 10.10 Maverick Meerkat [SOLVED] 'expr' command problems For some reason I'm unable to get expr to work with any parameters on strings that contain spaces. For example, This works: Code: $ temp='Fantastic' $ expr substr $temp 1 3 Fan Whereas this doesn't: Code: $ temp='Fantastic work' $ expr substr $temp 1 3 expr: syntax error The only difference is the added space. This problem occurs for all parameters of expr, including the length parameter. Suggestions welcome. Adv Reply June 19th, 2008 #2 pedro_orange View Profile View Forum Posts Private Message Extra Foam Sugar Free Ubuntu Join Date Nov 2007 Location UK Beans 772 DistroUbuntu 8.04 Hardy Heron Re: 'expr' command problems temp = "white space in string" echo temp: $temp Disclaimer: Yes I usually talk crap Adv Reply June 19th, 2008 #3 Richard_ View Profile View Forum Posts Private Message Spilled the Beans Join Date Jun 2008 Location Durban Beans 13 Di