using smbclient in bash script

James Yale jim at
Wed Jan 2 08:01:27 MST 2013

On 2 January 2013 01:05, lux11 <linuxone11 at> wrote:
> I have googled this subject extensively and found several examples where
> authors have advised that using variables is possible in a smbclient command
> string (using -c) and this MAY work from terminal but not within a bash
> script.
> Here is what I am trying to do: retrieve a log file from a windows machine
> on my LAN and copy the contents to a file on my linux box called
> "abclog.txt".
> Here is my bash script with an example of what DOES work and what does not.
> ---
> #! /bin/bash
> DATE=$(date +"%Y%m%d")
> FILENAME='"ABC logname '$DATE'.log"'
> #EXAMPLE 1 - this DOES work
> smbclient -A authfile //windows_machine_IP/C$ -c 'cd \\ABC\"ABC Logs"\Logs;
> get "ABC Logs 20121231.log" abclog.txt'
> #EXAMPLE 2 - this does NOT work
> smbclient -A authfile //windows_machine_IP/C$ -c 'cd \$DIRECTORY; get
> $FILENAME abclog.txt'
> ---
> The problem in example 2 is that smbclient simply looks for something called
> $DIRECTORY instead of interpreting the variable as defined. Is there a way
> to overcome this or perhaps a suggestion for a different approach ?
> Thanks,
> Lux

Example 2 works fine here if you replace your single quote (') with
double quotes (") so the variable is expanded rather than treated
literally. You'll also need to do something about the backslash in
front of your $DIRECTORY - if you need a literal backslash you'll need
to escape the backslash (with another backslash) or just remove it.

smbclient -A authfile //windows_machine_IP/C$ -c "cd $DIRECTORY; get >
$FILENAME abclog.txt"

More information about the smb-clients mailing list