1

Either it is late in the day for me or I am missing something naive here.

here is a contrived example

#!/bin/bash
command="ls -al > check.txt"
$command

When I run this script on a shell it gives me error I guess due to the ">" operator. Anyway I can redirect the output from inside a shell script. I thought this was very straight forward:

ls -la > temp.txt
ls: cannot access >: No such file or directory
ls: cannot access temp.txt: No such file or directory

2 Answers 2

4
#!/bin/bash
command="ls -al" 
$command > check.txt

> is a special character in Bash (and most shells). It does not belong to a command.

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

Comments

2

Here is another way to do it using eval,

#!/bin/bash
command="ls -al > check.txt"
eval $command

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.