Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 26th November 2008, 02:18 AM
youhaodeyi Offline
Registered User
 
Join Date: May 2007
Posts: 217
How does bash handle array variable?

I have a variable with string type and want to get its last character. How can I do this in bash?
Reply With Quote
  #2  
Old 26th November 2008, 02:43 AM
Camberwell's Avatar
Camberwell Online
Registered User
 
Join Date: Mar 2006
Location: Wallingford, Oxfordshire, UK
Age: 34
Posts: 294
hi there,

I've just done a quick google and found this...

http://www.faqs.org/docs/abs/HTML/st...ipulation.html

does that help?
Reply With Quote
  #3  
Old 26th November 2008, 02:50 AM
youhaodeyi Offline
Registered User
 
Join Date: May 2007
Posts: 217
Quote:
Originally Posted by Camberwell View Post
hi there,

I've just done a quick google and found this...

http://www.faqs.org/docs/abs/HTML/st...ipulation.html

does that help?
This page requires authentication. Could you please paste it on this forum?
Reply With Quote
  #4  
Old 26th November 2008, 02:52 AM
marcrblevins's Avatar
marcrblevins Offline
Registered User
 
Join Date: Jun 2006
Location: Texas
Age: 42
Posts: 4,168
You can grab the abs_guide.pdf from my site, listed in my sig. Bash god doc.
Reply With Quote
  #5  
Old 26th November 2008, 02:53 AM
Camberwell's Avatar
Camberwell Online
Registered User
 
Join Date: Mar 2006
Location: Wallingford, Oxfordshire, UK
Age: 34
Posts: 294
this page does not require authentication

but anyhow you probably want something like this...

string=sometext
echo `expr "$string" : '.*\(.\)'`

should print "t"
Reply With Quote
  #6  
Old 26th November 2008, 02:54 AM
wintersm Offline
Registered User
 
Join Date: Jul 2006
Posts: 293
the site works, but

Code:
${string:position:length}
Code:
   1 stringZ=abcABC123ABCabc
   2 #       0123456789.....
   3 #       0-based indexing.
   4 
   5 echo ${stringZ:0}                            # abcABC123ABCabc
   6 echo ${stringZ:1}                            # bcABC123ABCabc
   7 echo ${stringZ:7}                            # 23ABCabc
   8 
   9 echo ${stringZ:7:3}                          # 23A
  10                                              # Three characters of substring.

Last edited by wintersm; 26th November 2008 at 02:55 AM. Reason: smilie
Reply With Quote
  #7  
Old 26th November 2008, 03:14 AM
marcrblevins's Avatar
marcrblevins Offline
Registered User
 
Join Date: Jun 2006
Location: Texas
Age: 42
Posts: 4,168
Your answer is the last line of my junk file.

Code:
[marc@kiriyamablevins ~]$ cat junk
#!/bin/sh

stringZ=abcABC123ABCabc

echo ${#stringZ}                 # 15
echo `expr length $stringZ`      # 15
echo `expr "$stringZ" : '.*'`    # 15

echo ${stringZ:7:3}                          # 23A

echo `expr substr $stringZ 1 2`              # ab
echo `expr substr $stringZ 4 3`              # ABC
echo `expr substr $stringZ ${#stringZ} 1`    # c
[marc@kiriyamablevins ~]$ ./junk
15
15
15
23A
ab
ABC
c
[marc@kiriyamablevins ~]$
Reply With Quote
  #8  
Old 26th November 2008, 03:30 AM
youhaodeyi Offline
Registered User
 
Join Date: May 2007
Posts: 217
OK got it. thanks.
Reply With Quote
  #9  
Old 26th November 2008, 03:34 AM
RupertPupkin's Avatar
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 4,616
To get the last character in a string variable called x, use this: ${x:${#x}-1}
Code:
$ x='this is a long string!'
$ echo "${x:${#x}-1}"
!
Reply With Quote
Reply

Tags
array, bash, handle, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash $?: what is the $? variable? gold Using Fedora 2 1st January 2008 04:27 AM
Bash Array Planescape Programming & Packaging 7 25th January 2007 08:41 AM
[bash-script] regex variable carramba Programming & Packaging 1 1st July 2006 05:46 PM
bash can't handle spaces? Firewing1 Using Fedora 3 23rd October 2005 08:34 PM


Current GMT-time: 01:10 (Wednesday, 22-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat