Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-16-2012, 08:39 PM   #1
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Rep: Reputation: 17
Need help with script writing: Storing cmd in variable, print variable, then exe cmds

I have a script which contains sections that store a command to a variable, prints out the command, then executes it.

MYCOMMAND="cp file1.txt $NEWFILE"
echo "Running $MYCOMMAND"

The above works when I save a single command to the string like I did above. However I want to store a string made up of multiple commands separated by && or ;. Example:

MYCOMMAND="cp file1.txt $NEWFILE && rm file1.txt && echo done"
echo "Running $MYCOMMAND"
When I separate the commands with && or ;, the whole string is echoed correctly, but only the first command actually gets executed. So the cp in the above example get executed, the rm and 'echo done' are not run.

How I can save a string with multiple commands in it to a single variable, echo that variable so the commands that are going to be run are printed out first, and then actually run the commands stored in that variable?
Old 01-16-2012, 09:31 PM   #2
LQ Newbie
Registered: Aug 2010
Posts: 5

Rep: Reputation: 1
MYCOMMAND="cp file1.txt $NEWFILE ; rm file1.txt ; echo done"
echo "Running $MYCOMMAND"
echo $MYCOMMAND |sh
1 members found this post helpful.
Old 01-16-2012, 11:50 PM   #3
Valery Reznic
ELF Statifier author
Registered: Oct 2007
Posts: 676

Rep: Reputation: 136Reputation: 136
You can use 'set -x'

Either this way if you want to trace every command in your script:
#!/bin/bash -x

your script
Or this way for only selected commands:
set -x
# those commands will be traced
set +x

# those commands will not be traced
Old 01-17-2012, 12:26 AM   #4
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Originally Posted by fsainsbu View Post
MYCOMMAND="cp file1.txt $NEWFILE ; rm file1.txt ; echo done"
echo "Running $MYCOMMAND"
echo $MYCOMMAND |sh
Nice technique

But, if any of the command arguments include whitespace (space, tab or newline), it will fail. More information about wanting to execute complex commands held in a bash variable in Greg's Bash FAQ.


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
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 07:13 AM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
Trying to write a perl script that will print shell variable ohcarol Programming 2 04-16-2007 08:02 AM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 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