LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-26-2011, 05:40 PM   #1
lityit
LQ Newbie
 
Registered: Oct 2011
Posts: 8

Rep: Reputation: Disabled
is it possible to execute more than one command from inside a variable in BASH?


For example if i want to execute 3 commands and i want to store all 3 of these commands in a variable like so:

Code:
 variable="echo hello && mkdir uhoh && echo goodbye"
I would get this when trying to execute it:
Code:
$variable
hello && mkdir uhoh && echo goodbye
It only pays attention to the first command and echo's the rest. But what if I want it to execute all three commands, is there any way to do this?

Last edited by lityit; 10-26-2011 at 05:49 PM.
 
Old 10-26-2011, 06:23 PM   #2
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Quote:
Originally Posted by lityit View Post
For example if i want to execute 3 commands and i want to store all 3 of these commands in a variable like so:

Code:
 variable="echo hello && mkdir uhoh && echo goodbye"
I would get this when trying to execute it:
Code:
$variable
hello && mkdir uhoh && echo goodbye
It only pays attention to the first command and echo's the rest. But what if I want it to execute all three commands, is there any way to do this?
Yes, you need to use eval:
Code:
variable="echo hello && mkdir uhoh && echo goodbye"

eval "$variable"
I've found this useful when you want to echo the command being run before running it:
Code:
#!/bin/sh

some_command="ls -l /"

echo "Running $some_command"

eval "$some_command"
It keeps you from having to type in the command twice, and if you need to change it you need only change it in one place.

Lyle.
 
1 members found this post helpful.
Old 10-26-2011, 07:16 PM   #3
lityit
LQ Newbie
 
Registered: Oct 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lyle_s View Post
Yes, you need to use eval:
Code:
variable="echo hello && mkdir uhoh && echo goodbye"

eval "$variable"
I've found this useful when you want to echo the command being run before running it:
Code:
#!/bin/sh

some_command="ls -l /"

echo "Running $some_command"

eval "$some_command"
It keeps you from having to type in the command twice, and if you need to change it you need only change it in one place.

Lyle.
thanks
 
Old 10-26-2011, 11:40 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Personally I would try and create a function instead of using a variable as eval has its own little issues and the eventual need to escape all in sundry
becomes a real pain.
 
Old 10-27-2011, 12:06 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I fully agree with grail, especially since both Bash and POSIX shells allow you to define a function practically anywhere. You can do things like
Code:
setfoo () {
    if [ "$1" = "y" ]; then
        foo () {
            echo "foo: yes"
        }
    else
        foo () {
            echo "foo: no"
        }
    fi
}
so that for example
Code:
setfoo y
foo
setfoo n
foo
will output
Code:
foo: yes
foo: no
If you want a niftier example, define function setfoo like this:
Code:
setfoo () {
    eval "foo () {
        echo \"$*:\" \"\$@\"
    }"
}
The eval there is needed since we want $* from the outer function. To make sure "$@" is not evaluated within the eval, we escape it to "\$@".

Then, running
Code:
setfoo a b c
foo 1 2 3
foo 3 2 1

setfoo d e f
foo 4 5 6
foo 6 5 4
will output
Code:
a b c: 1 2 3
a b c: 3 2 1

d e f: 4 5 6
d e f: 6 5 4
 
Old 10-27-2011, 10:29 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Variables are designed for storing data, not code. So in general you shouldn't put commands in variables at all. As the others have pointed out, storing code is what functions are designed for.

http://mywiki.wooledge.org/BashFAQ/050

At best variables can, and perhaps even should, be used to hold some of the arguments to a command. And even then you should use an array to store them if you have multiple options to string together.

Edit: Oh, and see here for details on the security implications of eval:

http://mywiki.wooledge.org/BashFAQ/048

90% of the time there are better ways to do what you want anyway.

Last edited by David the H.; 10-27-2011 at 10:32 AM. Reason: as stated
 
  


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
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
Execute command with spaces from variable in bash script klo_2k Linux - Newbie 4 04-13-2008 02:59 AM
Execute bash script inside PHP creatorrr Programming 11 11-21-2007 04:32 PM
[Bash] Command inside a variable yvovandoorn Programming 5 01-20-2007 06:48 PM
very new to C, how to execute command inside C? neurotic85 Programming 5 09-05-2005 08:19 AM

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

All times are GMT -5. The time now is 01:46 AM.

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