Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-26-2007, 11:20 AM   #1
LQ Newbie
Registered: Jun 2006
Posts: 20

Rep: Reputation: 0
Running commands from array in a child bash script

Hi guys, kind of a newbie question, so I apologize in advance.
First I tried to export an array from one script to it's child, and I figured it's not supported in bash (would be happy if someone can correct me). So I pass the array cells one by one. It works for all commands except for those with redirection signs (| > etc.), see example and error below. Your help is appreciated!!
CMDS=("ls -l > /tmp/log")
~/ "${CMDS[0]}"
for i in "$@"
if [ "$i" ]; then
echo "$i"

ls -l > /tmp/log
ls: >: No such file or directory
ls: /tmp/log: No such file or directory
Old 10-26-2007, 12:33 PM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63 takes each of the strings passed on the command line and tries to execute them as a program. However, it does not split them with IFS. You want to "eval $i", like this
# this is

for i in "$@"
        if [ "$i" != "" ]; then
                echo "$i"
                eval $i
By the way, you can put ode fragments in [code] tags - this will preserve formatting and use a fixed width font to aid readability.

Regarding he exporting of arrays: environment variables are passed between processes as an array of strings only.

Last edited by matthewg42; 12-15-2007 at 06:19 PM.
Old 10-26-2007, 02:16 PM   #3
LQ Newbie
Registered: Jun 2006
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks a
Man !


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
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 06:51 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:55 AM.

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