Review your favorite Linux distribution.
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 04-14-2006, 03:17 AM   #1
Registered: Nov 2003
Distribution: Debian
Posts: 164

Rep: Reputation: 30
Bash variable substitution


My purpose is to compare the output of two commands with diff. I am trying to do this by assigning the two commands to two variables wherafter I use the variables with diff. Please let me know if I should use another approach to achieve my goal, I am new to bash programming.

The problem I have run into is that I have tried to assign a for loop to a variable like this:

name="for i in unzip; do dpkg -L $i; done"
but I get the next error when I do "$name":

-bash: for i in unzip ; do dpkg -L  ; done: command not found
Can someone show me what I am doing wrong?

Thanks for your help.

Old 04-14-2006, 04:18 AM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
It's not entirely clear to me what it is you want the script to do exactly. Anyways:
You need to prevent that $i is substituted ("expanded" in shell-script parlance) at the first assignment of the string. One way to do that is putting a backslash in fron of the $. Like this:
name="for i in unzip; do dpkg -L \$i; done"
Also, to have the shell (bash) execute the string, use eval, otherwise bash will treat the entire string (including whitespace) as a single command:
eval $name
Old 04-14-2006, 04:20 AM   #3
Senior Member
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,290

Rep: Reputation: 62
My programming is not the best, but if you want the value of a variable to contain a executed code try removing the double quotes and use this one instead and see if it will work:

name=`for i in unzip; do dpkg -L $i; done`
Old 04-14-2006, 02:16 PM   #4
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Hey thanks guys, your explanation helped me real well.

To explain what my goal is please assume there is another command assigned to a variable. For example:

name2="for i in gcc; do dpkg -L \$i; done"

Now I want to compare the output of this command and the previous command we mentioned with diff.

I have tried:

diff $name $name2

diff "$name" "$name2"

but these output errors.

So what command should I use to compare the output of the two commands?




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
Variable substitution (stupid noob) question in tcsh King of Men Linux - Newbie 4 12-04-2005 10:12 AM
Bash Process Substitution joshholt Programming 4 10-11-2005 04:15 AM
bash assignments of commands substitution p0rtius Programming 6 07-03-2005 07:41 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 05:38 PM
shell question variable substitution dpressm Programming 9 11-18-2004 08:00 AM > Forums > Non-*NIX Forums > Programming

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