LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-11-2008, 11:37 AM   #1
Agentrooker
LQ Newbie
 
Registered: Sep 2003
Posts: 9

Rep: Reputation: 5
Executing a string in bash


I've got a script that goes through some nasty regex stuff to produce a text file, which I then make executable and run from within the script like this:

./foo

But I'd like to find a more elegant way of doing it. Now let's suppose I'm storing my file to be executed to $FOO instead of to a file. What would be the best way to run the commands listed in that variable?

I've heard of using the "`" special character to run commands in bash, with something like:

`echo $FOO`

But I haven't gotten that to work. Recently, I tried just piping the standard output to bash via:

echo $FOO | bash

And this has been working, but I have a feeling I'm overlooking something more elegant. Any suggestions?
 
Old 08-11-2008, 11:42 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Try:
Code:
eval $foo
 
Old 08-11-2008, 12:10 PM   #3
Agentrooker
LQ Newbie
 
Registered: Sep 2003
Posts: 9

Original Poster
Rep: Reputation: 5
Ah, that's exactly the kind of elegant thing I was looking for. Thanks. That being said, for whatever reason, it seems to be clipping my line breaks, so I'll have to modify my script to place "&&" between each command.
 
Old 08-11-2008, 12:15 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Use semicolons. Assigning to a variable, the shell converts newlines output into spaces.
Corrected: the shell converts newlines into spaces upon word splitting, unless protected by quotes.

Be careful to sanitize your code, and that any eval'd code is safe.

Last edited by Mr. C.; 08-11-2008 at 03:06 PM.
 
Old 08-11-2008, 02:47 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Alternatively:
Code:
eval "$foo"
 
Old 08-11-2008, 03:04 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Quote:
Originally Posted by Mr. C. View Post
Use semicolons. Assigning to a variable, the shell converts newlines output into spaces.
jlliagre's fine suggestion forced me to rethink my response above. Newline stripping is not done during assignment; it is performed during possible future word-splitting, which the double quotes disable.
 
  


Reply

Tags
bash


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
Hi, I am a complete n00b in need of help executing her bash shell script. savoirefaire Linux - Newbie 4 03-12-2007 06:14 AM
executing Bash Scripts c4nk Linux - General 3 03-11-2007 04:52 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
Trivial bash question - executing $COMMAND atom Programming 4 06-05-2005 04:17 AM
Bash problem, no executing of string ChoKamir Programming 3 01-31-2005 04:50 AM


All times are GMT -5. The time now is 03:33 AM.

Main Menu
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