LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-01-2008, 10:42 PM   #1
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
bash: execute a semicolon separated list of commands within string


Could someone explain this behavior and how to fix it.

Code:
#! /bin/bash

cmd="echo ONE; echo TWO; echo THREE;"
echo RUN
$cmd
produces this:
Code:
RUN
ONE; echo TWO; echo THREE;
I obviously want three echos.
 
Old 10-01-2008, 11:03 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Use eval $cmd

The quotes protected the semicolons.
 
Old 10-01-2008, 11:04 PM   #3
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 66
If you're not married you can have my first born daughter.

Quote:
Originally Posted by Mr. C. View Post
The quotes protected the semicolons.
I figured that but I didn't know how to un-protect them. \; didn't work
 
Old 10-01-2008, 11:25 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
The shell assigned the entire string. Then, when you ran:

$cmd

The shell expanded the string, and tries to execute the first word, using the remaining words as arguments. Thus, you get:

echo ONE; echo TWO; echo THREE;

This occurs because your command is a simple command expansion:

man bash:
Quote:
SIMPLE COMMAND EXPANSION
When a simple command is executed, the shell performs the following
expansions, assignments, and redirections, from left to right.

1. The words that the parser has marked as variable assignments
(those preceding the command name) and redirections are saved
for later processing.

2. The words that are not variable assignments or redirections are
expanded. If any words remain after expansion, the first word
is taken to be the name of the command and the remaining words
are the arguments
.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM
How to modify bash to allow to execute some specified commands only ? nitin_ucer Linux - Security 5 08-16-2007 03:33 PM
[Bash] execute string as command michael_hk Linux - Newbie 2 06-26-2006 03:34 AM
Execute bash commands from web page solomage Linux - Software 1 10-07-2004 10:54 AM
BASH scripting; su to user, commands after dont execute Zero-0-Effect Programming 4 06-07-2004 11:09 AM


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

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