The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Basic bash 'for loop' usage Orange Stripes Shell Programming and Scripting 2 12-18-2007 08:58 PM
Basic OS question catwomen HP-UX 4 09-08-2006 08:39 PM
how to reset failed login in solaris 8.0 karthikosu SUN Solaris 2 07-12-2006 03:07 PM
basic question urwannabefriend UNIX for Dummies Questions & Answers 1 04-10-2004 05:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2007
er_ashu er_ashu is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 50
A basic question of FOR loop

Hi,
have a basic query.
Please see the below code:

list="one two three"
for var in $list ; do
echo $var
list="nolist"
Done

Wht if I want to print only first/ last line in the list

Eg one & three

Regards
er_ashu
  #2 (permalink)  
Old 09-28-2007
ajcannon ajcannon is offline
Registered User
  
 

Join Date: Aug 2007
Location: Binfield, Berkshire. UK
Posts: 91
awk

don't know if it is the best way but awk will work....

echo $variable | awk '{print $1,$NF}'

note $NF represents the no of fields
  #3 (permalink)  
Old 09-28-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
with zsh and array:

Code:
zsh 4.3.4%zsh 4.3.4% list=(one two three)
zsh 4.3.4% print $list[1]
one
zsh 4.3.4% print $list[-1]
three
otherwise:

Code:
$ list="one two three"
$ printf "${list%% *}\n"
one
$ printf "${list##* }\n"
three
  #4 (permalink)  
Old 09-28-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
with bash
Code:
# var=(one two three)
# echo ${var[0]}
one
# echo ${var[2]}
three
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0