Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-16-2009, 09:48 AM   #1
LQ Newbie
Registered: Oct 2009
Posts: 1

Rep: Reputation: 0
How to extract the last digit from an input parameter???

Hi, I'm looking to find out how do I write a bash script that will take the last digit from a command and display it? Basically I have my script and I want it to take the last number from the date,extract it and place it on a separate line on its own.

This is what I have so far:


echo 'NEST 304 UNIX Operations'
echo 'Marcus Ramsawak'
echo '803327030'
echo `date`

I'm doing this for school and I'm stuck, I hope I can get some hints and not the answer flat out.

Old 10-16-2009, 09:52 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Welcome to LQ!!-----You are the latest "poster child" for the RIGHT way to ask a homework question.

Take a look at the "cut" command--in particular, the "-c" option.

Also "tr"

"man cut" or "man tr" for details
Old 10-16-2009, 12:44 PM   #3
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,402
Blog Entries: 1

Rep: Reputation: 64
yes, it s difficult to figure out how many chars are in the string and then, getting the last.
It is more easy to get the first one, isn't ? well, reverse it first, so the last becomes the first.
Guess what is the name of a command that reverse a string ?
Old 10-16-2009, 01:23 PM   #4
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It can be done entirely in the shell (that is without using external commands such as cut and tr) by using Shell Parameter Expansion in conjunction with Arithmetic Expansion. That's not easy so a hint is that it uses ${parameter:offset}


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
C++: Extract numbers from an input file, find av & max, min programmernew Programming 4 10-27-2008 10:00 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Testing parameter input to perl script merana Programming 1 03-17-2006 09:54 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration