21

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.

  • 3
    You can also write [ -f "/usr/bin/wine" ] && export WINEARCH=win32 – glenn jackman Jan 20 '17 at 17:40
  • 1
    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
35

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
  • 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
  • 4
    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
  • @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 9 at 10:42
  • @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 at 14:24
2

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.

  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.