10
echo hello; echo hi

is identical to this in a script:

echo hello
echo hi

How do I do the same to the following possibilities?

echo hello && echo hi
echo hello || echo hi

I presume I can do this:

echo hello && \
echo hi

echo hello || \
echo hi 

But this is more or less imitating a multi-line script

What is the appropriate way of doing this?

edit: I am aware of how && and || work, I am just wondering how to effectively replicate it in a multi-line script

Miati
  • 405
  • 1
  • 5
  • 13
  • 1
    || executes the second statement only when the first statement returns failure. – jim mcnamara Dec 20 '14 at 23:15
  • I see your edit, and apologize for the oversight. Take a look at my edit. – MeetTitan Dec 21 '14 at 00:03
  • 4
    Using the `&&` or `||` operator between two commands is call a **compound expression** or **compound command**. The operators are a `test construct` between the commands on either side. `&&` says do the `second` only if the `first` succeeds. `||` says do the `second` only if the `first` fails. **NOTE** no line continuation is required after either -- it is implied. Remove `\\` from your code above and confirm. – David C. Rankin Dec 21 '14 at 00:44
  • 3
    It is really beyond my understanding why this question got closed. In my opinion, it is very clear and has a fair point which I now would like to know the answer to too. – phil294 May 21 '17 at 22:37
  • Anyway, [this question](http://stackoverflow.com/questions/821396/aborting-a-shell-script-if-any-command-returns-a-non-zero-value) is fairly similar. – phil294 May 21 '17 at 22:40
  • In my script file, I did what you presumed you can do but without the `\` and it worked well. I was then able to add comments for each command. – Eric Majerus Jun 05 '18 at 14:31
  • I think I found the answer in here! https://superuser.com/a/1188001/1284856 – Jerry Mar 14 '21 at 14:15

2 Answers2

2

You can use if:

if echo hello; then
    echo hi
fi

if ! echo hello; then
    echo hi
fi
Ry-
  • 218,210
  • 55
  • 464
  • 476
2

echo one && echo two prints one, and if that returns 0 it prints two, too.

echo one || echo two prints one, and if that doesn't return 0 prints two, too.

You can use it to shorten if statements, like so:

if [[ 1 -eq 2 ]]; then doStuff; fi shortens to

[[ 1 -eq 2 ]] && doStuff

Another example would be my startx script I run to update, then startx on my Ubuntu machine

sudo apt-get update && sudo apt-get -y upgrade && apt-get -y autoremove && startx

What that does is chain && together (remember all && does is check if the command "piped" to it exits with 0) in a way that only the next command is run if the last is run without errors. I do this so that if for some reason apt fails me, I can fix it from my terminal without waiting for xorg to do it's thing.

EDIT:

If you want to "expand" &&, to more lines use:

commandToCheckReturnOf
if [[ $? -eq 0 ]]; then
    doStuff
fi

If you're after ||, then:

commandToCheckReturnOf
if [[ $? -ne 0 ]]; then
    doStuff
fi

Sorry for the misunderstanding.

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
  • I'm not sure if your examples mimic && or ||, but [[]] may be the best method. Eg. command; [[ $? == 0 ]] && command or [[ $? == 0 ]] || command – Miati Dec 20 '14 at 23:36
  • It is more pseudo code. `[[ 1 -eq 2 ]]` is obviously always false, and `doStuff` isn't defined, but try plugging some stuff into my example and see for yourself what the difference is. For example, what does `[[ 1 -eq 2 ]] || echo Hello` give you? :-) – MeetTitan Dec 20 '14 at 23:42
  • 2
    If you're familiar with `$?`, think of `&&` as the shorthand of `if [[ $? -eq 0 ]]; then command; fi`. And `||` would be `if [[ $? -ne 0 ]]; then command; fi`. They're not exactly the same but enough so to help you understand the concept. – MeetTitan Dec 20 '14 at 23:50