![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to use substr to return data into a shell script variable? | ttunell | Shell Programming and Scripting | 3 | 07-23-2008 01:02 AM |
| Substr in shell script | gjithin | Shell Programming and Scripting | 7 | 05-10-2008 05:40 PM |
| substr() thru awk Korn Shell Script | sbryant | Shell Programming and Scripting | 4 | 01-24-2008 11:16 AM |
| shell script string extension | vbm | Shell Programming and Scripting | 4 | 11-10-2006 01:58 AM |
| Trouble using substr function with Bourne shell script | E2004 | Shell Programming and Scripting | 4 | 09-29-2005 11:57 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
substr from a string in Shell script
Shell Scripting Gurus,
I am having a hard time trying to figure out what I am doing wrong in my script. Below is the script snippet. It gives an error when it tries to execute the expression.a=`expr substr $stringZ 5 10` #!/bin/bash echo "Hello" stringZ="abcABC123ABCabc" echo $stringZ echo "Length is ${#stringZ}" echo `expr "$stringZ" : '.*'` a=`expr substr $stringZ 5 10` echo $a output : Hello abcABC123ABCabc Length is 15 15 expr: syntax error $ What am I doing wrong? Why is it giving syntax error when it executes a=`expr substr $stringZ 5 10` Can one of you gurus out there help me fix this? Thanks a lot! Ajay. |
|
||||
|
I think this should rather have been addressed by era, the herder of Randal's useless use of cat awards? I would here say, contender for useless use of echo and backticks award ![]() Quote:
The nifty bash substr expansion seems to start counting chars from naught (which seems logical to programmers) whereas the expr substr seems indexing from 1 (which I would say is more Fortran style, if I remember correctly) Code:
$ str=abcABC123ABCabc
$ echo ${str:5:10}
C123ABCabc
$ expr substr $str 5 10
BC123ABCab
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|