What is the difference between double-ampersand (&&) and semicolon (;) in Linux Bash?
The &&
operator is a boolean AND operator: if the left side returns a non-zero exit status, the operator returns that status and does not evaluate the right side (it short-circuits), otherwise it evaluates the right side and returns its exit status. This is commonly used to make sure that command2
is only run if command1
ran successfully.
The ;
token just separates commands, so it will run the second command regardless of whether or not the first one succeeds.
Different between ; and && in bash
;
will execute the second command whether or not the first returns without error.&&
is the bash AND
logical operator, and will execute the second command only if the first returns succesfully without error.
The success of a command is determined by its exit status.
difference between semicolon and double ampersand in makefile
see https://serverfault.com/a/373053/85018 for a good explanation:
The ; just separates one command from another. The && says only run the following command if the previous was successful
What does the semicolon do when it is run in a bash command?
The ;
separates the two commands.
echo a; echo b
It lets the bash know that echo a
and echo b
are two separate commands and need to be run separately one after the other
Try without semicolons
$ echo a echo b
a echo b
Here the statement is taken as a single command echo
and a echo b
is passed as parameter to the echo
Difference between a semicolon and a new line in bash ( source rcfile; foo vs. source rcfile NEWLINE foo where foo is an alias defined in rcfile)
As in your comment:
alias sudo='sudo -i'
is in.bashrc
file
According to man bash
:
The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at
least one complete line of input, and all lines that make up a compound command, before executing any
of the commands on that line or the compound command. Aliases are expanded when a command is read,
not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias
definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function
is executed, because a function definition is itself a command. As a consequence, aliases defined in
a function are not available until after that function is executed. To be safe, always put alias
definitions on a separate line, and do not use alias in compound commands.For almost every purpose, aliases are superseded by shell functions.
Difference between ;& and ;; in a bash script
From man bash
:
Using ;& in place of ;; causes execution to continue with the list
associated
with the next set of patterns.
Since ;&
occurs on the last pattern in the case
statement, it should make no difference in that script.
nohup doesn't work when used with double-ampersand (&&) instead of semicolon (;)
See Bash's Operator Precedence.
The &
is being attached to the whole statement because it has a higher precedence than &&
. You don't need ssh
to verify this. Just run this in your shell:
$ sleep 100 && echo yay &
[1] 19934
If the &
were only attached to the echo yay
, then your shell would sleep for 100 seconds and then report the background job. However, the entire sleep 100 && echo yay
is backgrounded and you're given the job notification immediately. Running jobs
will show it hanging out:
$ sleep 100 && echo yay &
[1] 20124
$ jobs
[1]+ Running sleep 100 && echo yay &
You can use parenthesis to create a subshell around echo yay &
, giving you what you'd expect:
sleep 100 && ( echo yay & )
This would be similar to using bash -c
to run echo yay &
:
sleep 100 && bash -c "echo yay &"
Tossing these into an ssh
, and we get:
# using parenthesis...
$ ssh localhost "cd / && (nohup sleep 100 >/dev/null </dev/null &)"
$ ps -ef | grep sleep
me 20136 1 0 16:48 ? 00:00:00 sleep 100
# and using `bash -c`
$ ssh localhost "cd / && bash -c 'nohup sleep 100 >/dev/null </dev/null &'"
$ ps -ef | grep sleep
me 20145 1 0 16:48 ? 00:00:00 sleep 100
Applying this to your command, and we get
ssh server "cd /tmp/path && (nohup java server 0</dev/null 1>server_stdout 2>server_stderr &)"
or:
ssh server "cd /tmp/path && bash -c 'nohup java server 0</dev/null 1>server_stdout 2>server_stderr &'"
Also, with regard to your comment on the post,
Right,
sh -c
always returns 0. E.g.,sh -c exit 1
has error code
0"
this is incorrect. Directly from the manpage:
Bash's exit status is the exit status of the last command executed in
the script. If no commands are executed, the exit status is 0.
Indeed:
$ bash -c "true ; exit 1"
$ echo $?
1
$ bash -c "false ; exit 22"
$ echo $?
22
What is the difference between command1 && command2 and command1 & command2 in Linux cmd, to execute chain commands?
&&
is a logical AND operator, so in the first case command1
is started and if it succeeds (returns with exit status of 0) then command2
will run.
&
is an operator in bash/shell that starts the command backgrounded, so in the second case command1
is started in the background then (without waiting for it to finish) command2
is started.
difference between #./ and #. ./
./foo
executed foo
if it's marked as executable and has a proper shebang line (or is an ELF binary). It will be executed in a new process.
. ./foo
or . foo
loads the script in the current shell. It is equal to source foo
With your example code you need to use the second way if you want the exported variables to be available in your shell.
Related Topics
How to Search Contents of Multiple PDF Files
How Find Out Which Process Is Using a File in Linux
Can't Detach Child Process When Main Process Is Started from Systemd
Centos 64 Bit Bad Elf Interpreter
How to Get a List of Available Wireless Networks on Linux
How to Discover What Linux Distribution Is in Use
Using Interrupt 0X80 on 64-Bit Linux
What Are Libtool's .La File For
Shell - Write Variable Contents to a File
How to Use Ioctl() to Manipulate My Kernel Module
How Is the Linux Kernel Tested
Difference Between Shell and Environment Variables
How Does Bash Deal with Nested Quotes
Vim Configuration for Linux Kernel Development
Where Are Include Files Stored - Ubuntu Linux, Gcc
Does Gcc Have Any Options to Add Version Info in Elf Binary File