Abhi
Abhi

Reputation: 6255

redirecting stdout in shell script

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

Upvotes: 1

Views: 391

Answers (2)

dpp
dpp

Reputation: 1758

Here is another way to do it using eval,

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

Upvotes: 2

m0skit0
m0skit0

Reputation: 25863

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

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

Upvotes: 4

Related Questions