LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-30-2007, 12:12 PM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
executing Bourne shell variable as command?


Assuming I have dynamically constructed a string representing a command, how do I then execute the command? This must be simple, but I haven't been able to figure out the syntax...

Code:
#!/bin/sh
cmd='pwd'
`$cmd`          # wrong syntax
Any help would be appreciated. Thanks.
 
Old 07-30-2007, 12:16 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Use "eval $cmd".
 
Old 07-30-2007, 12:41 PM   #3
ocicat
Member
 
Registered: May 2007
Posts: 208

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by Nylex
Use "eval $cmd".
Works like a champ! Thanks!
 
Old 07-31-2007, 02:00 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
"$cmd" also works as will just $cmd
 
  


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
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
Write output or command to variable (shell) kernel_geek Programming 9 03-12-2007 04:45 AM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 12:33 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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