1

How can I execution a bash line(command) that contains the redirection characters? For instance, the following line can't be execution.

home>CMD="ls -l > out" home>$CMD ls: cannot access >: No such file or directory ls: cannot access out: No such file or directory 

Thanks for your help in advance.

2 Answers 2

4

eval $CMD will do what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

eval is a bug magnet, and should be avoided whenever possible. If you must use it, at least double-quote the argument (i.e. eval "$CMD") to avoid the really bizarre behavior that can come from having the command run through extra levels of parsing.
4

You could use eval, but it is not recommended. If the purpose of storing the command in a variable is to execute it multiple times, it is better to use a function.

Example:

dols() { ls -l > out } dols 

See also: I'm trying to put a command in a variable, but the complex cases always fail!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.