Bash if on single line

39

7

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

Posted 2017-01-20T15:05:37.413

Reputation: 541

3You can also write [ -f "/usr/bin/wine" ] && export WINEARCH=win32 – glenn jackman – 2017-01-20T17:40:00.290

3Keep 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 – 2017-01-20T21:40:32.447

Answers

64

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

dr01

Posted 2017-01-20T15:05:37.413

Reputation: 2 509

1Well that was easy... :) Now I feel kinda awkward for asking that. Thank you for the answer tho, it helped a lot! – Horațiu Mlendea – 2017-01-20T17:03:29.890

5There 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 – 2018-03-10T12:22:16.953

@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 – 2019-03-09T10:42:29.307

@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 – 2019-03-12T14:24:01.333

3

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

Posted 2017-01-20T15:05:37.413

Reputation: 31

3Note 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 – 2017-01-31T22:11:09.437