Problem with variable assignment


Forum: Programming and Scripting
Topic: Problem with variable assignment
started by: rlr0304

Posted by rlr0304 on Jan. 18 2007,07:19
I'm trying to assign the results of ${myStr:(-1)} (which returns the last character of the myStr) to a variable.  I've tried:

  lastChar=${myStr:(1)}
  lastChar='${myStr:(1)}'
  lastChar=`${myStr:(1)}`
  lastChar="${myStr:(1)}"

and a number of other variations.  I've haven't been able to get it to work.

Can someone please help me out?

TIA,

Richard

Posted by mikshaw on Jan. 18 2007,13:18
If this is Bash, Your first attempt should have worked:
lastChar=${myStr:(-1)}
Another option is to add a space after the colon:
lastChar=${myStr: -1}

If you're using some other shell other than Bash, this probably won't work, and you may need to pipe your variable through cut or some other program.

Posted by rlr0304 on Jan. 19 2007,08:46
Thanks for the reply.  Turned it part of my problem was a typing error that I didn't see despite reading it a hundred times.
Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.