132

I would like to know how can I write if conditions inside a bash script on a single line.

For example, how can I write this on a single line, and then put another one just like it on the next?

if [ -f "/usr/bin/wine" ]; then
    export WINEARCH=win32
fi

I ask this because I have quite a few aliases in my .bashrc and I have the same .bashrc (synced) on multiple systems, but I don't need all aliases on each systems. I put most of them inside if statements, and it is all working beautifully now but they take a lot of space, 3 lines each plus the blank line between them (i like having them easily visible)

I will also use this technique for environment variables as well.

Horațiu Mlendea
  • 1,481
  • 3
  • 11
  • 9
  • 5
    You can also write `[ -f "/usr/bin/wine" ] && export WINEARCH=win32` – glenn jackman Jan 20 '17 at 17:40
  • 7
    Keep in mind that shorter is not necessarily better. You can get subtle logic errors with the shell's logic operators. Choose what's easier to read and maintain. – glenn jackman Jan 20 '17 at 21:40
  • Totally agree on choosing what is easier to read (chopping out clue words like "if" and "then" can be a recipe for disaster). Having a single conditional on a single line is almost always easier to read and maintain than something sprawled over three lines. – JonathanDavidArndt Sep 18 '21 at 03:22

4 Answers4

201

You would write it as such:

if [ -f "/usr/bin/wine" ]; then export WINEARCH=win32; fi

Note that this could also be written (as suggested by @glennjackman):

[ -f "/usr/bin/wine" ] && export WINEARCH=win32
dr_
  • 4,210
  • 11
  • 34
  • 46
  • 1
    Well that was easy... :) Now I feel kinda awkward for asking that. Thank you for the answer tho, it helped a lot! – Horațiu Mlendea Jan 20 '17 at 17:03
  • 25
    There is one difference. 1. expression will exit with 0 status, 2. expression will exit with non 0 status if file dont exist. This can make difference if you use this in some automated tasks like deployment where task success depends on exit status of command. – Jānis Gruzis Mar 10 '18 at 12:22
  • 2
    @HorațiuMlendea obviously it was easy. if you know how. But the syntax is fiddly e.g. [1==1] is an error but [ 1==1 ] works. And the semi-colons. How can you feel awkward for not knowing some fiddly badly designed syntax. Your 'feeling' makes no sense at all. This was a useful question, even if unfortunately you don't understand why. – barlop Mar 09 '19 at 10:42
  • 2
    @balop no, I totally see what you mean and I agree. It's just that at the time I understood the individual bits that made up that syntax (the one with &&). Usually it's easy and intuitive to put things together, but as you said, this language is pretty fiddly and not very intuitive. – Horațiu Mlendea Mar 12 '19 at 14:24
5

I also find that just typing any complex if then else command, hit enter, and then after it executes just hit the up arrow. The command line will repeat the last typed command of course but in this case it puts it all on one line as you require. It's a cheat way, but it's effective.

Alan
  • 51
  • 1
  • 6
    Note that this might depend on your shell (and possibly further on your shell's settings). Some quick testing on my computer shows this working in Bash (so have a +1), but Zsh preserves the line breaks when returning to previous commands. – 8bittree Jan 31 '17 at 22:11
2

To test and handle both the true and false the conditions on one line you can do this:

[ -f "/usr/bin/wine" ] && export WINEARCH=win32 || echo "No file"

You can include an exit code also:

[ -f "/usr/bin/wine" ] && export WINEARCH=win32 || exit 1

However, avoid including more than one command after the or operator because it will always be executed:

# This will always return an exit status of 1 even if the first condition is true
[ -f "/usr/bin/wine" ] && export WINEARCH=win32 || echo "No file" && exit 1
abk
  • 121
  • 2
1

If you need a little bit of more complexity, you may also use () to group statements and fine detail the operation:

[ -f "/usr/bin/wine" ] && (grep -Exiq /usr/bin/wine || echo "Foo" >> /usr/bin/wine)

In this case, it will not create the file /usr/bin/wine if it does not exists previously, whereas not using () will create it.

RicHincapie
  • 111
  • 3