Bash if on single line
Solution 1
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
Solution 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.
Related videos on Youtube
Horațiu Mlendea
Updated on September 18, 2022Comments
-
Horațiu Mlendea over 1 year
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.
-
glenn jackman over 7 yearsYou can also write
[ -f "/usr/bin/wine" ] && export WINEARCH=win32
-
glenn jackman over 7 yearsKeep 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.
-
JonathanDavidArndt over 2 yearsTotally 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.
-
-
Horațiu Mlendea over 7 yearsWell that was easy... :) Now I feel kinda awkward for asking that. Thank you for the answer tho, it helped a lot!
-
8bittree over 7 yearsNote 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.
-
Jānis Gruzis about 6 yearsThere 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.
-
barlop about 5 years@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.
-
Horațiu Mlendea about 5 years@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.