Search code examples
bashlftp

Output LFTP result to variable


How do I save LFTP result in a variable so I can use it later in my script.

This is the basic command I have:

lftp -c 'open -e "mirror /path/to/remote /path/to/local/" ftp://username:password@ftp.domain.com:21'

This obviously this doesn't work:

#!/bin/bash

output=""
lftp -c 'open -e "mirror /path/to/remote /path/to/local/" ftp://username:password@ftp.domain.com:21' > $output
echo "Test: " $output

EDIT:

It seems the problem is using lftp -c doesn't create any output. Therefore the variable is empty. So the problem is to get output from lftp.


Solution

  • Use Command Substitution to store the output of a command in a variable:

    output=`lftp -c 'open -e "mirror /path/to/remote /path/to/local/" ftp://username:password@ftp.domain.com:21'`
    echo "Test: ${output}"