tags:

views:

76

answers:

4

I wonder what the difference between

"echo 'hello'; ls"

and

"echo 'hello' && ls"

is? they both do the same thing

+3  A: 

Here, they do the same.

But take an other example:

cp file target && ls
cp file target; ls

In the first case, ls would only be executed if cp succeeds. In the second case, no matter if cp fails or succeeds, ls will always be executed.

Lekensteyn
As DarkDust and telent mention, echo can indeed fail, although it's usually not something the script would care about or test for.
gamen
+7  A: 

The && is the logical AND operator. The idea in its use in command1 && command2 is that command2 is only evaluated/run if command1 was successful. So here ls will only be run if the echo command returned successful (which will always be the case here, but you never know ;-). You could also write this as:

if echo 'hello'
then
  ls
fi

The semicolon just delimits two commands. So you could also write echo 'hello' ; ls as:

echo 'hello'
ls

Thus ls will also be executed even when echo fails.

BTW, successful in this context means that the program was exited with something like exit(0) and thus returned 0 as a return code (the $? shell variable tells you the return status of the last executed command).

DarkDust
+1 Nice answer.
Dennis Williamson
+5  A: 

So here ls will only be run if the echo command returned successful (which will always be the case here, but you never know ;-).

Well, not always. For example, the standard output stream may be unwritable

:; ( echo "foo" && touch /tmp/succeeded) >/dev/full
bash: echo: write error: No space left on device
:; ls -l /tmp/succeeded
ls: cannot access /tmp/succeeded: No such file or directory

(This should have been a comment not an answer, but stackoverflow won't let me post comments until I have more karma. Sorry)

telent
He, good point ! I haven't thought of that.
DarkDust
+1: Get closer to that karma level :)
Alexander Pogrebnyak
+1 I think this deserves to be an answer rather than a comment since it's hard to post code in comments adequately sometimes.
Dennis Williamson
+2  A: 

"echo 'hello' && ls" means : execute "ls" if "echo 'hello'" runs successfully. To understand what is "successful" in bash. Try this :

bash> cd /
bash> echo $?

if the previous command runs successfully, you should see 0

After that, try this :

bash> asdfdf
bash> echo $?

You should see a random value between 1 and 255. This means previous command didn't run successfully

On the other hand, "echo 'hello'; ls" means execute "ls" whether "echo 'hello'" runs successfully or not.

jancrot
No, *no*, **no** it will *not* be a random value. It's a very *specific* value. It's a "command not found" error and `echo $?` will give you 127 in Bash, zsh, ksh, dash and ash and probably others. Other values will be *specific* to the type of error (but they may vary in some cases from one shell to another). The value is *never*, *ever* **random**.
Dennis Williamson
Thanks for the correction. It's indeed exit status of previous command and it is never random. I wonder where the exit status list is documented. Hmmm..
jancrot