SSH with Command Doesn't Run as an Alias
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!
shell ssh quoting alias function
add a comment |
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!
shell ssh quoting alias function
add a comment |
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!
shell ssh quoting alias function
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!
shell ssh quoting alias function
shell ssh quoting alias function
edited Oct 25 '18 at 18:02
Jeff Schaller♦
43.9k1161141
43.9k1161141
asked Oct 25 '18 at 17:43
mattdondersmattdonders
1084
1084
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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 inalias
commands. Thanks so much!
– mattdonders
Oct 25 '18 at 18:53
It's not different escape characters in thealias
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
add a comment |
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 aboutxargs
& good to know about thels
piped - thanks so much!
– mattdonders
Oct 26 '18 at 13:48
add a comment |
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.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477802%2fssh-with-command-doesnt-run-as-an-alias%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 inalias
commands. Thanks so much!
– mattdonders
Oct 25 '18 at 18:53
It's not different escape characters in thealias
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
add a comment |
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 inalias
commands. Thanks so much!
– mattdonders
Oct 25 '18 at 18:53
It's not different escape characters in thealias
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
add a comment |
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.
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.
answered Oct 25 '18 at 18:09
RalfFriedlRalfFriedl
5,44031025
5,44031025
The second one worked - didn't realize there were different escape characters inalias
commands. Thanks so much!
– mattdonders
Oct 25 '18 at 18:53
It's not different escape characters in thealias
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
add a comment |
The second one worked - didn't realize there were different escape characters inalias
commands. Thanks so much!
– mattdonders
Oct 25 '18 at 18:53
It's not different escape characters in thealias
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
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
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
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
add a comment |
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 aboutxargs
& good to know about thels
piped - thanks so much!
– mattdonders
Oct 26 '18 at 13:48
add a comment |
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 aboutxargs
& good to know about thels
piped - thanks so much!
– mattdonders
Oct 26 '18 at 13:48
add a comment |
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.
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.
edited Oct 26 '18 at 6:05
answered Oct 26 '18 at 5:41
dave_thompson_085dave_thompson_085
2,10211111
2,10211111
This is a great alternative solution as well (and is much easier to read). I always forget aboutxargs
& good to know about thels
piped - thanks so much!
– mattdonders
Oct 26 '18 at 13:48
add a comment |
This is a great alternative solution as well (and is much easier to read). I always forget aboutxargs
& good to know about thels
piped - thanks so much!
– mattdonders
Oct 26 '18 at 13:48
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
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 8 at 15:53
answered Oct 25 '18 at 18:03
fra-sanfra-san
1,8771620
1,8771620
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477802%2fssh-with-command-doesnt-run-as-an-alias%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown