Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

bad substitution error in ksh

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-03-2011
LiorAmitai LiorAmitai is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 10 January 2012, 3:42 AM EST
Posts: 43
Thanks: 16
Thanked 2 Times in 1 Post
Tools bad substitution error in ksh

Hello,

In bash I can use the following:
TMP=12345
MID=${TMP:1:1}

the expected result is: 2

but when using KSH I'm getting a ''bad substitution" error.

What is the correct syntaxin ksh?

Thanks
Sponsored Links
    #2  
Old Unix and Linux 01-03-2011
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 28 August 2015, 4:20 PM EDT
Location: Amsterdam
Posts: 10,422
Thanks: 358
Thanked 2,765 Times in 2,471 Posts
In ksh93 that would work too, but not in ksh88. You can use this:

Code:
TMP=12345; TMP2=${TMP%${TMP#??}}
MID=${TMP2#?}

The Following User Says Thank You to Scrutinizer For This Useful Post:
LiorAmitai (01-03-2011)
Sponsored Links
    #3  
Old Unix and Linux 01-06-2011
LiorAmitai LiorAmitai is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 10 January 2012, 3:42 AM EST
Posts: 43
Thanks: 16
Thanked 2 Times in 1 Post
I just found another way to implement is is ksh using awk:

tmp=123456
echo $tmp | awk '{print substr($tmp,2,2)}'


result: 23
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
vi substitution drareeg Shell Programming and Scripting 2 11-07-2010 07:07 PM
bad substitution Error while renaming Extension paragkhanore Shell Programming and Scripting 6 06-09-2010 12:45 PM
sed substitution tom.lee Shell Programming and Scripting 2 10-02-2009 09:37 AM
Difference between "Command substitution" and "Process substitution" royalibrahim Shell Programming and Scripting 3 12-15-2008 10:20 AM
Variable substitution error cosec Shell Programming and Scripting 4 12-12-2007 03:40 AM



All times are GMT -4. The time now is 06:16 PM.