#Bash
Bash
Factoid:
###Factoid: TheThe extremely serious ShellShock bug was present in Bash since 1989, and remained undiscovered for a quarter of a century. Much of the joy of writing Bash is coming to grips with its many idiosyncracies and inconsistencies.
###Length 1 snippet:
Length 1 snippet:
Length 2 snippet:
||
###Length 2 snippet:
||
LikeLike logical or in most languages, but for processes. Will execute the command after the || only if the command before it returns non-zero.
Length 3 snippet:
x=y
###Length 3 snippet:
x=y
AssignmentAssignment. Nice and predictable... but unlike most other languages, extra spaces aren't allowed. Which is kind of funny because you can stick extra spaces pretty much everywhere else between things in bash, just not around the =.
Length 4 snippet:
$IFS
###Length 4 snippet: $IFS Internal Field Separator - this variable affects how Bash splits data for many built-in actions, such as iterating in for loops and populating arrays from strings. Used correctly it can be very powerful; but more often it's the cause of subtle and unpredictable bugs.
Length 5 snippet:
${x^}
###Length 5 snippet: ${x^} SubstituteSubstitute the string in x, but with the first character capitalised! Such a frequently used feature that it has its own dedicated piece of language syntax.
Length 6 snippet:
x=($y)
###Length 6 snippet: x=($y) FillFill an array, x, from a string or list of elements y, splitting on whatever the IFS is currently set to - by default, whitespace. A very useful feature for advanced bash programming.
Length 7 snippet:
${x[y]}
###Length 7 snippet:
${x[y]}
ArraysArrays! But wait, what's that... y is a string, not a numerical index? Yes indeed, Bash supports associative arrays! Many people don't know this. You just need to declare -A x first.
Length 8 snippet:
${x##*,}
###Length 8 snippet:
${x##*,}
SubstituteSubstitute x, everything up until the last , character (or whatever you specify). Useful to get the last field of a csv - this is something you can't so easily do with cut, which only counts fields from the left. % and %% allows the same to cut from the right; % and # were chosen for their location on the US keyboard so it would be clear which means left and which means right, but that doesn't hold much value for everyone not using a US keyboard :)
Length 9 snippet:
[ a = b ]
###Length 9 snippet: [ a = b ] InIn most other languages, a single equals in a comparison operation would produce unintended behaviour in the form of an assignment. Not in Bash, though. Just don't omit any of the spaces, whatever you do!
Length 10 snippet:
if [ a=b ]
###Length 10 snippet:
if [ a=b ]
ThisThis is what happens if you forget about the mandatory spaces. Will not throw an error. Will always return true - even if a and b are variables that are unset, or whatever they're set to, doesn't matter - it'll always return true. Think about code like if [ "$password"="$correctpass" ] to see the fun potential of this "feature".
Length 11 snippet:
x=${y//a/b}
###Length 11 snippet: x=${y//a/b} RegexRegex-style substring replacement! Set x to the value of y but with every instance of a replaced with b.
Length 12 snippet:
[[:upper:]]*
###Length 12 snippet:
[[:upper:]]*
PatternPattern matching - you aren't limited to just using the * wildcard in the shell, you can use any POSIX standard match such as alnum, alpha, digit etc.
Length 13 snippet:
function x(){
###Length 13 snippet:
function x(){
AA bit of C syntax has mysteriously crept in! One of many completely different uses for curly braces, and another example of optional decorative elements to make Bash look more like other languages - either () or function can be omitted here (but not both). Also more fun with inconsistent spaces - a space after the { is mandatory, but not before the closing }, as in function x { y;}
Length 14 snippet:
echo {Z..A..3}
###Length 14 snippet: echo {Z..A..3} YetYet another totally unrelated use of curly braces. Expands a range with a specified step. In this case, will produce every 3rd letter from Z to A. Useful for generating sequences without using seq, but cannot be used with variables, so has limited functionality.
Length 15 snippet:
echo {a,b,c,d}x
###Length 15 snippet:
echo {a,b,c,d}x
AnotherAnother similar but not identical use for curly braces in sequence generation; prints ax bx cx dx, and is useful for generating a list of strings from a sequence or list in a single command. Again however, limited in usefulness as it can't be used with variables inside the braces.
#Bash
###Factoid: The extremely serious ShellShock bug was present in Bash since 1989, and remained undiscovered for a quarter of a century. Much of the joy of writing Bash is coming to grips with its many idiosyncracies and inconsistencies.
###Length 1 snippet:
###Length 2 snippet:
||
Like logical or in most languages, but for processes. Will execute the command after the || only if the command before it returns non-zero.
###Length 3 snippet:
x=y
Assignment. Nice and predictable... but unlike most other languages, extra spaces aren't allowed. Which is kind of funny because you can stick extra spaces pretty much everywhere else between things in bash, just not around the =.
###Length 4 snippet: $IFS Internal Field Separator - this variable affects how Bash splits data for many built-in actions, such as iterating in for loops and populating arrays from strings. Used correctly it can be very powerful; but more often it's the cause of subtle and unpredictable bugs.
###Length 5 snippet: ${x^} Substitute the string in x, but with the first character capitalised! Such a frequently used feature that it has its own dedicated piece of language syntax.
###Length 6 snippet: x=($y) Fill an array, x, from a string or list of elements y, splitting on whatever the IFS is currently set to - by default, whitespace. A very useful feature for advanced bash programming.
###Length 7 snippet:
${x[y]}
Arrays! But wait, what's that... y is a string, not a numerical index? Yes indeed, Bash supports associative arrays! Many people don't know this. You just need to declare -A x first.
###Length 8 snippet:
${x##*,}
Substitute x, everything up until the last , character (or whatever you specify). Useful to get the last field of a csv - this is something you can't so easily do with cut, which only counts fields from the left. % and %% allows the same to cut from the right; % and # were chosen for their location on the US keyboard so it would be clear which means left and which means right, but that doesn't hold much value for everyone not using a US keyboard :)
###Length 9 snippet: [ a = b ] In most other languages, a single equals in a comparison operation would produce unintended behaviour in the form of an assignment. Not in Bash, though. Just don't omit any of the spaces, whatever you do!
###Length 10 snippet:
if [ a=b ]
This is what happens if you forget about the mandatory spaces. Will not throw an error. Will always return true - even if a and b are variables that are unset, or whatever they're set to, doesn't matter - it'll always return true. Think about code like if [ "$password"="$correctpass" ] to see the fun potential of this "feature".
###Length 11 snippet: x=${y//a/b} Regex-style substring replacement! Set x to the value of y but with every instance of a replaced with b.
###Length 12 snippet:
[[:upper:]]*
Pattern matching - you aren't limited to just using the * wildcard in the shell, you can use any POSIX standard match such as alnum, alpha, digit etc.
###Length 13 snippet:
function x(){
A bit of C syntax has mysteriously crept in! One of many completely different uses for curly braces, and another example of optional decorative elements to make Bash look more like other languages - either () or function can be omitted here (but not both). Also more fun with inconsistent spaces - a space after the { is mandatory, but not before the closing }, as in function x { y;}
###Length 14 snippet: echo {Z..A..3} Yet another totally unrelated use of curly braces. Expands a range with a specified step. In this case, will produce every 3rd letter from Z to A. Useful for generating sequences without using seq, but cannot be used with variables, so has limited functionality.
###Length 15 snippet:
echo {a,b,c,d}x
Another similar but not identical use for curly braces in sequence generation; prints ax bx cx dx, and is useful for generating a list of strings from a sequence or list in a single command. Again however, limited in usefulness as it can't be used with variables inside the braces.
Bash
Factoid:
The extremely serious ShellShock bug was present in Bash since 1989, and remained undiscovered for a quarter of a century. Much of the joy of writing Bash is coming to grips with its many idiosyncracies and inconsistencies.
Length 1 snippet:
Length 2 snippet:
||
Like logical or in most languages, but for processes. Will execute the command after the || only if the command before it returns non-zero.
Length 3 snippet:
x=y
Assignment. Nice and predictable... but unlike most other languages, extra spaces aren't allowed. Which is kind of funny because you can stick extra spaces pretty much everywhere else between things in bash, just not around the =.
Length 4 snippet:
$IFS
Internal Field Separator - this variable affects how Bash splits data for many built-in actions, such as iterating in for loops and populating arrays from strings. Used correctly it can be very powerful; but more often it's the cause of subtle and unpredictable bugs.
Length 5 snippet:
${x^}
Substitute the string in x, but with the first character capitalised! Such a frequently used feature that it has its own dedicated piece of language syntax.
Length 6 snippet:
x=($y)
Fill an array, x, from a string or list of elements y, splitting on whatever the IFS is currently set to - by default, whitespace. A very useful feature for advanced bash programming.
Length 7 snippet:
${x[y]}
Arrays! But wait, what's that... y is a string, not a numerical index? Yes indeed, Bash supports associative arrays! Many people don't know this. You just need to declare -A x first.
Length 8 snippet:
${x##*,}
Substitute x, everything up until the last , character (or whatever you specify). Useful to get the last field of a csv - this is something you can't so easily do with cut, which only counts fields from the left. % and %% allows the same to cut from the right; % and # were chosen for their location on the US keyboard so it would be clear which means left and which means right, but that doesn't hold much value for everyone not using a US keyboard :)
Length 9 snippet:
[ a = b ]
In most other languages, a single equals in a comparison operation would produce unintended behaviour in the form of an assignment. Not in Bash, though. Just don't omit any of the spaces, whatever you do!
Length 10 snippet:
if [ a=b ]
This is what happens if you forget about the mandatory spaces. Will not throw an error. Will always return true - even if a and b are variables that are unset, or whatever they're set to, doesn't matter - it'll always return true. Think about code like if [ "$password"="$correctpass" ] to see the fun potential of this "feature".
Length 11 snippet:
x=${y//a/b}
Regex-style substring replacement! Set x to the value of y but with every instance of a replaced with b.
Length 12 snippet:
[[:upper:]]*
Pattern matching - you aren't limited to just using the * wildcard in the shell, you can use any POSIX standard match such as alnum, alpha, digit etc.
Length 13 snippet:
function x(){
A bit of C syntax has mysteriously crept in! One of many completely different uses for curly braces, and another example of optional decorative elements to make Bash look more like other languages - either () or function can be omitted here (but not both). Also more fun with inconsistent spaces - a space after the { is mandatory, but not before the closing }, as in function x { y;}
Length 14 snippet:
echo {Z..A..3}
Yet another totally unrelated use of curly braces. Expands a range with a specified step. In this case, will produce every 3rd letter from Z to A. Useful for generating sequences without using seq, but cannot be used with variables, so has limited functionality.
Length 15 snippet:
echo {a,b,c,d}x
Another similar but not identical use for curly braces in sequence generation; prints ax bx cx dx, and is useful for generating a list of strings from a sequence or list in a single command. Again however, limited in usefulness as it can't be used with variables inside the braces.
###Length 13 snippet:
function x(){
A bit of C syntax has mysteriously crept in! One of many completely different uses for curly braces, and another example of optional decorative elements to make Bash look more like other languages - either () which actually serves no purpose here andor function can be omitted, and is included only to make the syntax look more C-like here (but not both). Also more fun with inconsistent spaces - a space after the { is mandatory, but not before the closing }, as in function x { y;}
###Length 13 snippet:
function x(){
A bit of C syntax has mysteriously crept in! One of many completely different uses for curly braces, and another example of decorative elements () which actually serves no purpose here and can be omitted, and is included only to make the syntax look more C-like. Also more fun with inconsistent spaces - a space after the { is mandatory, but not before the closing }, as in function x { y;}
###Length 13 snippet:
function x(){
A bit of C syntax has mysteriously crept in! One of many completely different uses for curly braces, and another example of optional decorative elements to make Bash look more like other languages - either () or function can be omitted here (but not both). Also more fun with inconsistent spaces - a space after the { is mandatory, but not before the closing }, as in function x { y;}
###Length 14 snippet: echo {Z..A..3} Yet another totally unrelated use of curly braces. Expands a range with a specified step. In this case, will produce every 3rd letter from Z to A. Useful for generating sequences without using seq, but cannot be used with variables, so has limited functionality.
###Length 15 snippet:
echo {a,b,c,d}x
Another similar but not identical use for curly braces in sequence generation; prints ax bx cx dx, and is useful for generating a list of strings from a sequence or list in a single command. Again however, limited in usefulness as it can't be used with variables inside the braces.
###Length 14 snippet: echo {Z..A..3} Yet another totally unrelated use of curly braces. Expands a range with a specified step. In this case, will produce every 3rd letter from Z to A. Useful for generating sequences without using seq, but cannot be used with variables, so has limited functionality.
###Length 14 snippet: echo {Z..A..3} Yet another totally unrelated use of curly braces. Expands a range with a specified step. In this case, will produce every 3rd letter from Z to A. Useful for generating sequences without using seq, but cannot be used with variables, so has limited functionality.
###Length 15 snippet:
echo {a,b,c,d}x
Another similar but not identical use for curly braces in sequence generation; prints ax bx cx dx, and is useful for generating a list of strings from a sequence or list in a single command. Again however, limited in usefulness as it can't be used with variables inside the braces.