When to use ; and when to use && on the commandline

When to use ; and when to use && on the commandline

Post by Rudi Ahler » Mon, 23 Jun 2003 06:01:57



When do I use which? I've seen people running a few commands after each
other, with either of these in-between, what's the difference?

--

Kind Regards
Rudi Ahlers

Compassion is difficult to give away because it keeps coming back.

 
 
 

When to use ; and when to use && on the commandline

Post by Rob MacGrego » Mon, 23 Jun 2003 06:22:02



> When do I use which? I've seen people running a few commands after each
> other, with either of these in-between, what's the difference?

; separates commands just like a press of <RETURN>

&& means that the following command is only run if the previous command
returned 0 as the exit code.

Both will be explained (probably in more detail) in the man page of the
shell.

--
   Rob MacGregor (BOFH)        Oh my God! They killed init! You bastards!
       The light at the end of the tunnel is an oncoming dragon.

        Real email address is at Hotmail.  Put _ between my names.

 
 
 

When to use ; and when to use && on the commandline

Post by Rudi Ahler » Mon, 23 Jun 2003 06:26:21


Thanx

--

Kind Regards
Rudi Ahlers

Compassion is difficult to give away because it keeps coming back.


> > When do I use which? I've seen people running a few commands after each
> > other, with either of these in-between, what's the difference?

> ; separates commands just like a press of <RETURN>

> && means that the following command is only run if the previous command
> returned 0 as the exit code.

> Both will be explained (probably in more detail) in the man page of the
> shell.

> --
>    Rob MacGregor (BOFH)        Oh my God! They killed init! You bastards!
>        The light at the end of the tunnel is an oncoming dragon.

> Real email address is at Hotmail.  Put _ between my names.

 
 
 

When to use ; and when to use && on the commandline

Post by Hugo Lovhoide » Mon, 23 Jun 2003 06:49:37



> When do I use which? I've seen people running a few commands after each
> other, with either of these in-between, what's the difference?

Semi-colon separates commands, && is a logic AND. "cd tmp; ls" runs
both commands, one after the other, while "cd tmp && ls" will execute "ls"
only if the first succeeds.
--
Mvh Hugo
 
 
 

When to use ; and when to use && on the commandline

Post by Simon Barne » Mon, 23 Jun 2003 06:42:08


Hi,

Quote:> When do I use which? I've seen people running a few commands after each
> other, with either of these in-between, what's the difference?

When you use the semikolon to separate commands, then they commands will
executed sequentially (in c.s. this would be called composition of
commands).

Example: echo -n Hello; echo ", world!" prints:

   Hello, world!

When you use && (logical and) to separate commands, then each of the commands
will be treated as a boolean expression (true iff the command returns
zero and false otherwise). The shell will execute the commands from left to
right while the expression is true (i.e. all programms return a non-zero value):

Example:
   gcc -c source.c -o source.o && gcc -oprogram source.o

If the compilation of source.c (1st command is successfull, the also
do the link step (2nd command)

There is also a third option: || (logical or):

The shell will execute the commands from left to right while the
expression is false.

Example:
   some_command_that_might_not_work || echo "There was an error!"

If the first command works (returns zero), then everything is fine
(nothing is done anymore). Otherwise display an error message.

There are also special shell commands 'true' and 'false' (also in
/usr/bin), that do nothing but returning zero (a non-zero value
respecitively).

'true' for example can be usefull, if you write a shell script that
launches several commands and stops if there was an error (i.e. command1
&& command2 && ...)

But what, if you want to ignore an error indicated by command2, which
you cannot (or do not want to modify):

Then you simply say:

command1 && (command2 || true) && ...

Regards,
  Simon

 
 
 

When to use ; and when to use && on the commandline

Post by Brian K. Whit » Mon, 23 Jun 2003 13:30:13



> Hi,

>> When do I use which? I've seen people running a few commands after each
>> other, with either of these in-between, what's the difference?

> When you use the semikolon to separate commands, then they commands will
> executed sequentially (in c.s. this would be called composition of
> commands).

> Example: echo -n Hello; echo ", world!" prints:

>    Hello, world!

> When you use && (logical and) to separate commands, then each of the
> commands will be treated as a boolean expression (true iff the command
> returns zero and false otherwise). The shell will execute the commands
> from left to right while the expression is true (i.e. all programms return
> a non-zero value):

> Example:
>    gcc -c source.c -o source.o && gcc -oprogram source.o

> If the compilation of source.c (1st command is successfull, the also do
> the link step (2nd command)

> There is also a third option: || (logical or):

> The shell will execute the commands from left to right while the
> expression is false.

> Example:
>    some_command_that_might_not_work || echo "There was an error!"

> If the first command works (returns zero), then everything is fine
> (nothing is done anymore). Otherwise display an error message.

> There are also special shell commands 'true' and 'false' (also in
> /usr/bin), that do nothing but returning zero (a non-zero value
> respecitively).

> 'true' for example can be usefull, if you write a shell script that
> launches several commands and stops if there was an error (i.e. command1
> && command2 && ...)

> But what, if you want to ignore an error indicated by command2, which you
> cannot (or do not want to modify):

> Then you simply say:

> command1 && (command2 || true) && ...

> Regards,
>   Simon

My favorite use of true & false is to make decicion-making in shell
scripts easy to write and easy to read.

DEBUG=false
ASTUFF=false
BSTUFF=true

[ some condition ] && DEBUG=true
[ some condition ] && ASTUFF=true
[ some condition ] && BSTUFF=false

some
code
...

$ASTUFF && {
        $DEBUG && echo "doing A stuff..."
        some
        code

Quote:} || {

        $DEBUG && echo "skipping A stuff..."

Quote:}

$DEBUG && {
        echo "X=\"$X\""
        echo "Y=\"$Y\""
        echo "press enter: \c"
        read junk

Quote:}

some
code
...

$BSTUFF && {
        $DEBUG && echo "doing B stuff..."
        some
        code

Quote:} || {

        $DEBUG && echo "skipping B stuff..."
        some other
        code

Quote:}

--

+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx  Linux SCO  Prosper/FACTS AutoCAD  #callahans Satriani
 
 
 

When to use ; and when to use && on the commandline

Post by Brian K. Whit » Mon, 23 Jun 2003 14:43:32



> My favorite use of true & false is to make decision-making in shell
> scripts easy to write and easy to read.

> DEBUG=false
> ASTUFF=false
> BSTUFF=true

> [ some condition ] && DEBUG=true
> [ some condition ] && ASTUFF=true
> [ some condition ] && BSTUFF=false

> some
> code
> ...

> $ASTUFF && {
>    $DEBUG && echo "doing A stuff..."
>    some
>    code
> } || {
>    $DEBUG && echo "skipping A stuff..."
> }
> } <-- remove
> $DEBUG && {
>    echo "X=\"$X\""
>    echo "Y=\"$Y\""
>    echo "press enter: \c"
>    read junk
> }
> } <-- remove
> some
> code
> ...

> $BSTUFF && {
>    $DEBUG && echo "doing B stuff..."
>    some
>    code
> } || {
>    $DEBUG && echo "skipping B stuff..."
>    some other
>    code
> }
> } <-- remove

what the... there are extra "}" up there... I marked them "remove"
I know it's only pseudo-code but it's just annoying when you set something
down as an example, a reference, and it gets a typo or gets mangled by the
news reader or something...

--

+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx  Linux SCO  Prosper/FACTS AutoCAD  #callahans Satriani

 
 
 

When to use ; and when to use && on the commandline

Post by Brian K. Whit » Tue, 24 Jun 2003 14:49:28




>> My favorite use of true & false is to make decision-making in shell
>> scripts easy to write and easy to read.

>> DEBUG=false
>> ASTUFF=false
>> BSTUFF=true

>> [ some condition ] && DEBUG=true
>> [ some condition ] && ASTUFF=true
>> [ some condition ] && BSTUFF=false

>> some
>> code
>> ...

>> $ASTUFF && {
>>        $DEBUG && echo "doing A stuff..."
>>        some
>>        code
>> } || {
>>        $DEBUG && echo "skipping A stuff..."
>> }
>> } <-- remove
>> $DEBUG && {
>>        echo "X=\"$X\""
>>        echo "Y=\"$Y\""
>>        echo "press enter: \c"
>>        read junk
>> }
>> } <-- remove
>> some
>> code
>> ...

>> $BSTUFF && {
>>        $DEBUG && echo "doing B stuff..."
>>        some
>>        code
>> } || {
>>        $DEBUG && echo "skipping B stuff..."
>>        some other
>>        code
>> }
>> } <-- remove

> what the... there are extra "}" up there... I marked them "remove" I know
> it's only pseudo-code but it's just annoying when you set something down
> as an example, a reference, and it gets a typo or gets mangled by the news
> reader or something...

Someone kindly emailed me, informing me that in fact my original post does
not have those extra "}" in it.

Even now, when I look at it, they are there in the original, So I am left
to assume it is a bug in my news client, Pan2 in displaying that
particular post.

Can anyone else corroborate?

I just looked in google, and although one debug line got un-wrapped in a
way that i don't like and breaks my intended aesthetic, it does not happen
to break the proper syntax of the shell. Other than that, in the google
copy, there are no extra "}"

--

+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx  Linux SCO  Prosper/FACTS AutoCAD  #callahans Satriani

 
 
 

When to use ; and when to use && on the commandline

Post by Simon Barne » Tue, 24 Jun 2003 17:11:16


Hi Brian,

Quote:> Someone kindly emailed me, informing me that in fact my original post does
> not have those extra "}" in it.

> Even now, when I look at it, they are there in the original, So I am left
> to assume it is a bug in my news client, Pan2 in displaying that
> particular post.

This seems to be a bug in your newsreader. I see your original posting
as exspected.

Regards,
  Simon