This is an unabashed yes.
When one uses ssh
to execute a command on a remote server it performs some kind of fancy internal input/output redirection. In fact, I find this to be one of the subtly nicer features of OpenSSH. Specifically, if you use ssh
to execute an arbitrary command on a remote system, then ssh will map STDIN
and STDOUT
to that of the command being executed.
For the purposes of an example, let's assume you want to create a backup tarball, but don't want to, or can't, store it locally. Let's have a gander at this syntax:
$ tar -cf - /path/to/backup/dir | ssh remotehost "cat - > backupfile.tar"
We're creating a tarball, and writing it to STDOUT
, normal stuff. Since we're using ssh to execute a remote command, STDIN gets mapped to the STDIN
of cat
. Which we then redirect to a file.
Yes, there is a big difference. &&
is short-circuiting, so the subsequent command would be executed only if the previous one returned with an exit code of 0
.
Quoting from the manual:
expression1 &&
expression2
True if both expression1 and expression2 are true.
On the other hand, a script containing
expression1
expression2
would execute the second expression even if the first failed. (Unless you specified the script to exit on error by saying set -e
.)
EDIT: Regarding your comment whether:
command1; command2
is the same as:
command1
command2
The answer is usually. Bash parses an entire statement block before evaluating any of it. A ; doesn't cause the previous command to be evaluated. If the previous command were to have an effect on how the subsequent one would be parsed, then you'd notice the difference.
Consider a file containing aliases, lets call it alias
, with an entry:
alias f="echo foo"
Now consider a script containing:
shopt -s expand_aliases
source ./alias
f
and another one containing:
shopt -s expand_aliases; source ./alias; f
then you might think that both would produce the same output.
The answer is NO. The first one would produce foo
but the second one would report:
... f: command not found
To clarify further, it's not expand_aliases
that's causing the problem. The problem is due to the fact that a statement like:
alias f="echo foo"; f
would be parsed in one go. The shell doesn't really know what f
is, this causes the parser to choke.
Best Answer
It works the way it does because Unix is full-duplex. As Ritchie said in The UNIX Time-sharing System: A Retrospective:
[end quote]
That being said, there are some modern programs that eat up or discard any typeahead;
ssh
andapt-get
are two examples. If you type ahead while they're running, you may find that the first part of your input has disappeared. That could conceivably be a problem.