LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-27-2007, 01:40 PM   #1
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Rep: Reputation: 15
Bash array to function


Hi!

How can I manipulate an array in BASH?

For example. After the call of function a I want the first element in the array to have been removed. So the output after the call should be b c.
Code:
#!/bin/bash

function a()
{
    unset $1[0]
}

array=( "a" "b" "c" )
echo "${array[@]}" // Output is "a b c"
a "${array[@]}"
echo "${array[@]}" // Output is "a b c", but should be "b c"
Thanks!
 
Old 11-27-2007, 01:55 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
What exactly are you trying to do? $1 is not an array variable. You would delete an array index with 'unset array[0]'. And then the function doesn't get you much, as it's a one token reference to a one token command.
 
Old 11-27-2007, 01:59 PM   #3
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by digiot View Post
What exactly are you trying to do?
I'm trying to manipulate an array in a function where the array was given as an argument to the function.

In the example above I'm trying to remove the first element from the array named array.
 
Old 11-27-2007, 02:40 PM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by rejeep View Post
I'm trying to manipulate an array in a function where the array was given as an argument to the function.

In the example above I'm trying to remove the first element from the array named array.
Code:
bash 3.2.25(1)$ array=( "a" "b" "c" )
bash 3.2.25(1)$ echo "${array[@]}"
a b c
bash 3.2.25(1)$ a(){ unset "$1"[0];}
bash 3.2.25(1)$ a array
bash 3.2.25(1)$ echo "${array[@]}"
b c
 
Old 11-27-2007, 02:45 PM   #5
rejeep
Member
 
Registered: Mar 2007
Distribution: Gentoo
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks! =)
 
Old 11-27-2007, 03:05 PM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by rejeep View Post
Thanks! =)
Consider that after removing the first element,
the next time you run a array nothing will happen
(you'll have no more ${array[0]} element.
 
  


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
Perl - returning array from a function rose_bud4201 Programming 6 07-13-2007 02:02 AM
Array structs - passed to function cdog Programming 4 02-02-2006 04:07 PM
returning an array from a function.. javascript sonesay Programming 1 06-07-2004 06:28 AM
sending pointer array to function marek Programming 4 04-15-2004 05:46 PM
c++ function & array question shaggy112 Programming 1 05-24-2001 11:41 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration