Share your knowledge at the LQ Wiki.
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-19-2006, 05:25 AM   #1
LQ Newbie
Registered: Oct 2006
Location: UK
Distribution: 2.6.16-gentoo-r7
Posts: 3

Rep: Reputation: 0
running a command with args from a shell script

I'm sure this is easy, but it's been baffling me for an hour.
cmd1="ls -l /home/me/ > /home/me/outfile"
but, cmd1 doesn't work as I want, which to behave exactly as if I had done this:

ls -l /home/me/ > /home/me/outfile
thanks in advance!
Old 10-19-2006, 05:46 AM   #2
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Rep: Reputation: 15
Damn this one has me baffled. Note how echo $cmd1 yeilds exactly what it should...
Is this a bug or some expected behaviour?
EDIT: This works...
cmd1="ls -l /home/me/"
$cmd1 > /home/me/outfile

Last edited by greeklegend; 10-19-2006 at 05:48 AM.
Old 10-19-2006, 06:28 AM   #3
LQ Newbie
Registered: Oct 2006
Location: UK
Distribution: 2.6.16-gentoo-r7
Posts: 3

Original Poster
Rep: Reputation: 0
found the answer afterall:

cmd1="ls -l /home/me/ > /home/me/outfile"
eval $cmd1
Old 10-19-2006, 02:46 PM   #4
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
You could also use an array, e.g.

cmd1=(ls -l /home/me/ > /home/me/outfile)
This ${cmd1[*]} is slightly cumbersome, since bash only expands the first element given $cmd1. I use zsh, which expands $cmd1 to the entire array, which is a little more convenient.


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
running shell script: command not found tripat Red Hat 3 04-14-2010 02:21 PM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 02:38 AM
Running script as user that does not have shell sschueller Linux - General 1 04-24-2004 04:06 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 10:22 AM
c/c++ running a shell script? thermalpants Programming 3 05-12-2002 12:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:19 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
Open Source Consulting | Domain Registration