LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > *BSD
User Name
Password
*BSD This forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.

Notices


Reply
  Search this Thread
Old 05-14-2005, 09:47 AM   #1
aaronshi
LQ Newbie
 
Registered: May 2005
Posts: 1

Rep: Reputation: 0
Red face Question about referring a word in a command output


I am learning shell scripting now.
In Unix, excuting the following commands:
set d=`date`
echo "Today is $d[1]"

You will get the first word of DATE command output by using $d[1], which is the date.
But in linux (I am using redhat 4.0 enterprise), system doesn't recognize $d[1] referring to the first word, but showing like " Today is Sat May 14 09:50:34 EDT 2005[1] ".

So I am wondering what is the correct usage of referrence to a word in a wordlist, like the above situation.

Thanks a lot!
 
Old 05-14-2005, 11:05 AM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
The command you are talking about is a capabitlity of the csh not bash...

Check which shell you are using.
 
Old 05-14-2005, 10:47 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
In bash you could do the same thing:
d=(`date`)
echo "Today is ${d[0]}"

The variable d is an array.
${#d[@]} will return the number of elements in the array.
Arrays in bash are like sparse arrays. You could have d[0], d[1], d[2], d[10] elements.
In this case, ${#d[@]} would return 4 rather than eleven. Also, the indexes are 0 based.
 
  


Reply


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
a command/script for replacing a word with another jaakkop Programming 14 12-04-2005 10:21 AM
Question about output of dmesg command. sarajevo Linux - Security 3 09-09-2005 03:44 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM
when booting, just show GRUB word not command ratana LinuxQuestions.org Member Intro 2 05-06-2004 04:05 PM
grepping last word of output rajatgarg Programming 3 11-25-2003 10:41 AM

LinuxQuestions.org > Forums > Other *NIX Forums > *BSD

All times are GMT -5. The time now is 01:31 AM.

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