<< >> ( ) ; & | <space><space> <tab><tab> < > ( ) ; & | <space> <tab> < > ( ) ; & | <space> <tab> - Abuse the ternary operator,
((test)) &&&& cmd1 || cmd2or[ test ] &&&& cmd1 || cmd2, as much as possible.
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi
By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 }
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 } As nyuszika7h pointed out in the comments, this specific example could be shortened even further using case:
t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac As nyuszika7h pointed outAlso, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e.
{and}) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the commentsparent environment, this specificso assuming that all the example couldcommands can safely be shortened even further usingrun in a subshellcase(which isn't typical in any case), you can shorten the above code to this:t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esact=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using -eq for integers).
One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)- Also, prefer parentheses to braces
In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension - see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best. word splits and pathname-expands the contents of $f. Esp. bad if it starts with -
Note this caveat, especially in checking for null output or an undefined variable:
[[ $f ]]&&: # double quotes aren't needed inside [[, which can save chars [ "$f" = '' ]&&: <-- This is significantly longer [ -n "$f" ]&&: In all technicality, this specific example would be best with case ... in:
t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac So, the moral of this post is this:
- Abuse the boolean operators as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any contextalways use them instead of
if/if-else/etc. This also meansconstructs. - Use parentheses as much as possible and run as many commands in a subshellsegments as possible, in subshells because curly braces (i.e.
{and})parentheses are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:
t=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using `-eq` for integers)not reserved words.
- One more thing, avoid Avoid quotes whereas much as physically possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)
- In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions Check out
case. It drops two characters for free..in, butsince it isn't as robust in some cases (it'smay save quite a Bash extension &endash; see below for an example). Alsofew bytes, use the single equals argument rather than the double. It is a free character to dropparticularly in string matching.[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best. word splits and pathname-expands the contents of $f. Esp. bad if it starts with -
Note this caveat, especially in checking for null output or an undefined variable:
[[ $f ]]&&: # double quotes aren't needed inside [[, which can save chars [ "$f" = '' ]&&: <-- This is significantly longer [ -n "$f" ]&&:
In all technicality, this specific example would be best with
case ... in:t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac
- Abuse the boolean operators as much as possible, and always use them instead of if/if-else/etc. constructs.
- Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
- Avoid quotes as much as physically possible.
- Check out `case .. in`, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
< > ( ) ; & | <space> <tab>P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
EDIT: As manatwork pointed out, the double parenthesis test only works for integers. Also, indirectly, I found that you need to have whitespace surrounding the `==` operator. Corrected my post above.< > ( ) ; & | <space> <tab> I also was too lazy to recalculate the length of each segment, so I simply removed them. It should be easy enough to find a string length calculator online if necessary.
EDIT: As manatwork pointed out, the double parenthesis test only works for integers. Also, indirectly, I found that you need to have whitespace surrounding the == operator. Corrected my post above.
I also was too lazy to recalculate the length of each segment, so I simply removed them. It should be easy enough to find a string length calculator online if necessary.
- Abuse the ternary operator,
((test)) && cmd1 || cmd2or[ test ] && cmd1 || cmd2, as much as possible.
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi
By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 }As nyuszika7h pointed out in the comments, this specific example could be shortened even further using
case:t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac
- Also, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e.
{and}) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:
t=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using `-eq` for integers).
- One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)
- In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension &endash; see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best. word splits and pathname-expands the contents of $f. Esp. bad if it starts with -
Note this caveat, especially in checking for null output or an undefined variable:
[[ $f ]]&&: # double quotes aren't needed inside [[, which can save chars [ "$f" = '' ]&&: <-- This is significantly longer [ -n "$f" ]&&:
In all technicality, this specific example would be best with
case ... in:t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac
- Abuse the boolean operators as much as possible, and always use them instead of if/if-else/etc. constructs.
- Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
- Avoid quotes as much as physically possible.
- Check out `case .. in`, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
< > ( ) ; & | <space> <tab>EDIT: As manatwork pointed out, the double parenthesis test only works for integers. Also, indirectly, I found that you need to have whitespace surrounding the `==` operator. Corrected my post above.
I also was too lazy to recalculate the length of each segment, so I simply removed them. It should be easy enough to find a string length calculator online if necessary.
- Abuse the ternary operator,
((test)) && cmd1 || cmd2or[ test ] && cmd1 || cmd2, as much as possible.
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 } As nyuszika7h pointed out in the comments, this specific example could be shortened even further using case:
t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac Also, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e.
{and}) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:t=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using -eq for integers).
One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension - see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best. word splits and pathname-expands the contents of $f. Esp. bad if it starts with -
Note this caveat, especially in checking for null output or an undefined variable:
[[ $f ]]&&: # double quotes aren't needed inside [[, which can save chars [ "$f" = '' ]&&: <-- This is significantly longer [ -n "$f" ]&&: In all technicality, this specific example would be best with case ... in:
t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac So, the moral of this post is this:
- Abuse the boolean operators as much as possible, and always use them instead of
if/if-else/etc. constructs. - Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
- Avoid quotes as much as physically possible.
- Check out
case...in, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
< > ( ) ; & | <space> <tab> EDIT: As manatwork pointed out, the double parenthesis test only works for integers. Also, indirectly, I found that you need to have whitespace surrounding the == operator. Corrected my post above.
I also was too lazy to recalculate the length of each segment, so I simply removed them. It should be easy enough to find a string length calculator online if necessary.
- Abuse the ternary operator,
((test)) && cmd1 || cmd2or[ test ] && cmd1 || cmd2, as much as possible.
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi
By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 }As nyuszika7h pointed out in the comments, this specific example could be shortened even further using
case:t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac
- Also, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e.
{and}) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:
t=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using `-eq` for integers).
- One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)
- In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension &endash; see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best. word splits and pathname-expands the contents of $f. Esp. bad if it starts with -
Note this caveat, especially in checking for null output or an undefined variable:
[[ "$f"$f ]]&&: # double quotes aren't needed inside [[, which can save chars [ "$f" = '' ]&&: <-- This is significantly longer [ -n "$f" ]&&:
In all technicality, this specific example would be best with
case ... in:t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac
- Abuse the ternaryboolean operatoroperators as much as possible, and always use itthem instead of if/if-else/etc. constructs always.
- Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
- Avoid quotes as much as physically possible.
- Check out `case .. in`, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
< > ( ) ; & | <space> <tab>- Abuse the ternary operator,
((test)) && cmd1 || cmd2or[ test ] && cmd1 || cmd2, as much as possible.
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi
By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 }As nyuszika7h pointed out in the comments, this specific example could be shortened even further using
case:t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac
- Also, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e.
{and}) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:
t=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using `-eq` for integers).
- One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)
- In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension &endash; see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best
Note this caveat, especially in checking for null output or an undefined variable:
[[ "$f" ]]&&: [ "$f" = '' ]&&: <-- This is significantly longer
In all technicality, this specific example would be best with
case ... in:t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac
- Abuse the ternary operator as much as possible, and always use it instead of if/if-else/etc. constructs always.
- Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
- Avoid quotes as much as physically possible.
- Check out `case .. in`, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
< > ( ) ; & | <space> <tab>- Abuse the ternary operator,
((test)) && cmd1 || cmd2or[ test ] && cmd1 || cmd2, as much as possible.
Examples (length counts always exclude the top line):
t="$something" if [ $t == "hi" ];then cmd1 cmd2 elif [ $t == "bye" ];then cmd3 cmd4 else cmd5 if [ $t == "sup" ];then cmd6 fi fi
By using ternary operators only, this can easily be shortened to:
t="$something" [ $t == "hi" ]&&{ cmd1;cmd2 }||[ $t == "bye" ]&&{ cmd3;cmd4 }||{ cmd5 [ $t == "sup" ]&&cmd6 }As nyuszika7h pointed out in the comments, this specific example could be shortened even further using
case:t="$something" case $t in "hi")cmd1;cmd2;;"bye")cmd3;cmd4;;*)cmd5;[ $t == "sup" ]&&cmd6;esac
- Also, prefer parentheses to braces as much as possible. Since parentheses are a metacharacter, and not a word, they never require spaces in any context. This also means run as many commands in a subshell as possible, because curly braces (i.e.
{and}) are reserved words, not meta-characters, and thus have to have whitespace on both sides to parse correctly, but meta-characters don't. I assume that you know by now that subshells don't affect the parent environment, so assuming that all the example commands can safely be run in a subshell (which isn't typical in any case), you can shorten the above code to this:
t=$something [ $t == "hi" ]&&(cmd1;cmd2)||[ $t == "bye" ]&&(cmd3;cmd4)||(cmd5;[ $t == "sup" ]&&cmd6)
Also, if you can't, using parentheses can still minify it some. One thing to keep in mind is that it only works for integers, which renders it useless for the purposes of this example (but it is much better than using `-eq` for integers).
- One more thing, avoid quotes where possible. Using that above advice, you can further minify it. Example:
t=$something [ $t == hi ]&&(cmd1;cmd2)||[ $t == bye ]&&(cmd3;cmd4)||(cmd5;[ $t == sup ]&&cmd6)
- In testing conditions, prefer single brackets to double brackets as much as possible with a few exceptions. It drops two characters for free, but it isn't as robust in some cases (it's a Bash extension &endash; see below for an example). Also, use the single equals argument rather than the double. It is a free character to drop.
[[ $f == b ]]&&: # ... <-- Bad [ $f == b ]&&: # ... <-- Better [ $f = b ]&&: # ... <-- Best. word splits and pathname-expands the contents of $f. Esp. bad if it starts with -
Note this caveat, especially in checking for null output or an undefined variable:
[[ $f ]]&&: # double quotes aren't needed inside [[, which can save chars [ "$f" = '' ]&&: <-- This is significantly longer [ -n "$f" ]&&:
In all technicality, this specific example would be best with
case ... in:t=$something case $t in hi)cmd1;cmd2;;bye)cmd3;cmd4;;*)cmd5;[ $t == sup ]&&cmd6;esac
- Abuse the boolean operators as much as possible, and always use them instead of if/if-else/etc. constructs.
- Use parentheses as much as possible and run as many segments as possible in subshells because parentheses are meta-characters and not reserved words.
- Avoid quotes as much as physically possible.
- Check out `case .. in`, since it may save quite a few bytes, particularly in string matching.
P.S.: Here's a list of meta-characters recognized in Bash regardless of context (and can separate words):
< > ( ) ; & | <space> <tab>