Getting the last character of a string with ksh

I need to capture the last character in a string, and if it equals to 's', I need to take one action, if not, then another.

I have

str="tests"
echo ${str:(-1)}

 ch= ${str:(-1)}

if [ ch == 's' ]
then


else


fi

I get an error

${str:(-1)}: 0403-011 The specified substitution is not valid for this command.
LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
ksh does not support this format of variable editing.

Try this instead:

str="tests"
ch=${str#${str%?}}
echo  $ch


And why not good old "expr"?

str="tests"
ch=$(expr substr $str $(expr length $str) 1)
echo  $ch


The above uses only the shell builtin "expr" and no external program.

This will also work:

str="tests"
typeset -R1 ch
ch=$str
echo $ch


Here we use only the shell builtin "typeset"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
farzanjCommented:
Try this:

$ str='hello'
$ echo ${str:${#str}-1}

Open in new window

0
woolmilkporcCommented:
Sorry, "expr" is not a shell builtin. My mistake!

And this is shorter:

str="tests"
ch=$(expr substr $str ${#str}  1)
echo  $ch
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

YZlatAuthor Commented:
What is #str?
0
woolmilkporcCommented:
1) ${str#<pattern>} strips <pattern> off of the front of the variable's content.

2) ${str%<pattern>} strips <pattern> off the end of the variable's content.

3) ${str%?} strips off the last character of the content, so we can use format (2) to strip off the result of (3).
0
simon3270Commented:
Does

  ${str: -1}

work?  Note the space between the : and the -
0
woolmilkporcCommented:
${#str} contains the lenght of $str
0
YZlatAuthor Commented:
both

echo ${str:${#str}-1}

and

${str: -1}


gave me the same error
0
woolmilkporcCommented:
And this?

echo ${str#${str%?}}

?
0
TintinCommented:
The string functions available in ksh vary depending on what version you have.

This will work for all versions of ksh (and bash)

str='abcdefg'
echo $str | cut -c${#str}

Open in new window

0
simon3270Commented:
The ${str: -1} construction is not in ksh88, but is in ksh93.  Does AIX still use ksh88?
0
woolmilkporcCommented:
@simon3270:

AIX ships ksh88 as /usr/bin/ksh. ksh93 is also shipped, but as /usr/bin/ksh93.

/bin/ksh and /bin/sh are both hardlinked to /usr/bin/ksh which makes ksh88 kind of a default.

wmp
0
simon3270Commented:
Thanks @wmp.

So, ${str: -1} (and various other solutions) could be used if the first line of the script was changed to

    #!/usr/bin/ksh93

That said, Tintin's solution is the most portable, though you could also do

    ch=`echo $str | sed 's/^.*\(.\)$/\1/'`
0
woolmilkporcCommented:
This works in ksh88, ksh93 and bash:

echo ${str#${str%?}}
0
ozoCommented:
unless you have something like
str='***'
0
woolmilkporcCommented:
Yep.

echo "${str#${str%?}}"

but then also

ch=`echo "$str" | sed 's/^.*\(.\)$/\1/'`
echo "$ch"

( simon3270's solution).
0
ozoCommented:
quotes fix echo `"$str" | sed 's/^.*\(.\)$/\1/'`
but not "${str#${str%?}}"
(at least not in all versions of ksh88, ksh93 and bash)
0
woolmilkporcCommented:
Interesting.

ksh93 and bash require this:

echo "${str#"${str%?}"}"
0
simon3270Commented:
if it is the nested ${} that is causing the problem, you could (going back to the original request) have:
if [ "${str%?}s" = "$str" ]; then
  : string ends with s - do the required work
fi

Open in new window

0
YZlatAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.