12

I want to capture to a file the output of the ls command

ls >> lsOutput.log

This one works if executed in the command line. But when put inside a shell script (lsOutput.sh), returns

./lsOutput.sh: 3: ./lsOutput.sh: total: not found

lsOutput.sh code

#!/bin/sh

`ls -lrt` >> lsOutput.log
1
  • The backticks around the command in the script make it mean something very different than the plain command run from the command line.
    – vonbrand
    Mar 1, 2014 at 21:36

1 Answer 1

13

Just remove the backticks from your script:

#!/bin/sh

ls -lrt >> lsOutput.log

Otherwise, the command is executed, and then its output is substituted and executed.

For example:

echo date

will output: date, while

`echo date`

will output current date, i.e. it will first evaluate to date, and then executed, which is calling the program date itself.

2

Not the answer you're looking for? Browse other questions tagged .