141

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
  • 6
    You can also write [ -f "/usr/bin/wine" ] && export WINEARCH=win32 Jan 20, 2017 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. Jan 20, 2017 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. Sep 18, 2021 at 3:22

4 Answers 4

211

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
4
  • 1
    Well that was easy... :) Now I feel kinda awkward for asking that. Thank you for the answer tho, it helped a lot! Jan 20, 2017 at 17:03
  • 26
    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. Mar 10, 2018 at 12:22
  • 3
    @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, 2019 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. Mar 12, 2019 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.

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, 2017 at 22:11
5

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
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .