1

I have the following command to remote into a local server and tail -f the latest log file for an application that I have.

The command works perfectly fine from the command line -

ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'

The problem is that when I make it an alias (or even a function), it evaluates the completion of the ls -1r on my local machine and tries to pass that to the remote machine.

alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

function latestbotlogs {
    ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'
}

What syntax do I need to use such that the entire command gets evaluated on the remote machine that I am accessing via SSH.

Thanks in advance!

2

For the alias you need some escapes

alias latestbotlogs="ssh user@hostname 'tail -f \\\$\\(ls -1r \\~/Development/python/twitter-bot/logs/*.log \\| head -1\\)'"

or

alias latestbotlogs='ssh user@hostname '\''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'\'

The second version is easier, you don't have to think about all the operators you have to quote.

The function should work as it is.

  • The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much! – mattdonders Oct 25 '18 at 18:53
  • It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command. – RalfFriedl Oct 25 '18 at 19:57
1

An alternative to getting the somewhat complicated quote-escaping right:

 alias botlogs='ssh user@host "ls -r ~/whatever/*log | head -1 | xargs tail -f"'
 # if (selected) filename contains backslash or quotemark(s) 
 # need -d'\n' on GNU and I don't know good solution on other
 # this also fails if filename contains whitespace, but so did $( )

although I concur that the function should work without any hackery and in general functions are more consistent and flexible and just plain better than aliases.

And PS: when ls output is piped (or redirected) it always uses 1-column format, you don't need -1 here.

  • This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much! – mattdonders Oct 26 '18 at 13:48
0

You may set the alias by assigning a string wrapped in single quotes to latestbotlogs, as in:

alias latestbotlogs='ssh user@hostname '\''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'\'

where the expression \' is used to protect single quotes from the shell and have them concatenated in the resulting command. You can verify that they are preserved:

$ alias latestbotlogs
alias latestbotlogs='ssh user@hostname '\''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'\'''

While with your alias:

alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

the command substitution is evaluated immediately, upon definition. This happens because single quotes lose their special meaning when escaped (with \) or enclosed in double quotes ("). (Reference: Quoting in "The Open Group Base Specifications Issue 7, 2018 edition").
And this is the key: single quotes around the remote command are enough to protect it when the alias is invoked, but do not prevent the enclosed command to be substituted when the alias is created.

You can also use "'" as an alternative to \':

alias latestbotlogs='ssh user@hostname '"'"'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"'"

Or enclose the whole command in double quotes and add some escaping:

alias latestbotlogs="ssh user@hostname 'tail -f \$(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

In this last form, all the characters that retain a special meaning when double-quoted ($, ` and \) have to be escaped (here we have only a $).

Your function, instead, should just work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.