0

I am using below command to find a most recent file with name "candump"

ls *.log | grep "candump" | tail -n 1

The output is "candump-2018-04-19_131908.log"

I want to store the output filename to a variable in my shell script. I am using the below commands:

logfile = `ls *.log | grep "candump" | tail -n 1`

and

logfile = $(ls *.log | grep "candump" | tail -n 1)

However, both times I am getting the same error, "logfile: command not found". Am I doing something wrong? Any help is appreciated.

jww
  • 90,984
  • 81
  • 374
  • 818
Verma
  • 13
  • 1
  • 1
  • 2
  • A little search goes a long way. Possible duplicate of [How to set a variable to the output from a command in Bash?](https://stackoverflow.com/q/4651437/608639), [How can I assign the output of a command to a shell variable?](https://unix.stackexchange.com/q/16024/56041), [Storing output of command in shell variable](https://unix.stackexchange.com/q/4569/56041), etc. – jww Apr 22 '18 at 14:19

1 Answers1

6

You have to stick the variable name and its value (no space before and after the =).

Try :

logfile=$(ls *.log | grep "candump" | tail -n 1)
Sébastien S.
  • 1,424
  • 7
  • 14