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" Code:
$variable |
Quote:
Code:
variable="echo hello && mkdir uhoh && echo goodbye" Code:
#!/bin/sh Lyle. |
Quote:
|
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. |
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 () { Code:
setfoo y Code:
foo: yes Code:
setfoo () { Then, running Code:
setfoo a b c Code:
a b c: 1 2 3 |
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. |
All times are GMT -5. The time now is 03:09 PM. |