Avatar of YZlat
YZlatFlag for United States of America asked on

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.
Shell ScriptingUnix OS

Avatar of undefined
Last Comment
YZlat

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
woolmilkporc

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
farzanj

Try this:

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

Open in new window

woolmilkporc

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

And this is shorter:

str="tests"
ch=$(expr substr $str ${#str}  1)
echo  $ch
ASKER
YZlat

What is #str?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
woolmilkporc

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).
simon3270

Does

  ${str: -1}

work?  Note the space between the : and the -
woolmilkporc

${#str} contains the lenght of $str
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
YZlat

both

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

and

${str: -1}


gave me the same error
woolmilkporc

And this?

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

?
Tintin

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
simon3270

The ${str: -1} construction is not in ksh88, but is in ksh93.  Does AIX still use ksh88?
woolmilkporc

@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
simon3270

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/'`
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
woolmilkporc

This works in ksh88, ksh93 and bash:

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

unless you have something like
str='***'
woolmilkporc

Yep.

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

but then also

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

( simon3270's solution).
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ozo

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

Interesting.

ksh93 and bash require this:

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

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
YZlat

Thanks!