You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Create an array with initial values |`VARNAME=(value1 value2 value3)`|
| Push to an array |`VARNAME+(value)`|
| Access an array's element |`VARNAME[index]`|
| Remove first element from an array (shift) |`shift VARNAME`|
| Remove last element from an array (pop) |`shift -p VARNAME`|
| Get an array's length |`${#VARNAME[@]}`|
| Iterate over an array's values |`for value in VARNAME;`|
| Iterate over an array's values |`for value in $VARNAME;`|
| Get index of a value in an array |`${VARNAME[(i)value]}`|
| Get an array slice _after_ the specified index |`${VARNAME:index}`|
| Get an array slice _after_ the specified index |`${VARNAME:index:length}`|
| Check if a value is contained in an array |`if (( ${VARNAME[(i)value]} <= ${#VARNAME[@]} ));`|
| Check if an array is empty |`if [[ -z $VARNAME ]]`|
| Check if an array is not empty |`if [[ ! -z $VARNAME ]]`|
## Associative arrays (= maps / dictionaries)
Associate arrays are the equivalent of hash maps or dictionaries in many other programming languages: unlike arrays, they can use string keys, and these don't necessary have an order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a cheat sheet for how to perform various actions to ZSH, which can be tricky to find on the web as the syntax is not intuitive and it is generally not very well-documented.
| Iterate over a range (inclusive) |`for i in {from..to};`|
| Iterate over a list of filesystem items |`for i in globpattern;`|
| Iterate over a list of filesystem items, fail silently if no match found |`for i in globpattern(N);`|
## Examples cheat sheet
Return a value from within a function:
```zsh
functionadd() {
local sum=$(($1+$2))
echo$sum
}
functionadd_twice() {
local sum=$(add $1$2)# get the callee's STDOUT
local sum_twice=$(add $sum$sum)
echo$sum_twice
}
echo$(add 2 3)# 5
echo$(add_twice 2 3)# 10
```
## A word on conditionals
Conditionals use expressions, such as in `if [[ -z $VARNAME ]];` the expression is `[[ -z $VARNAME ]]`. These can also be used in `while` loops, as well as be used outside of blocks:
```zsh
[[ -z$VARNAME ]] &&echo"VARNAME is defined!"
[[ -f$FILEPATH ]] &&echo"File exists!"
```
This works because conditional expressions (`[[ ... ]]` and `(( ... ))`) don't actually return a value; they behave like commands and as such set the status code to `0` if the condition is true, or `1` else.
If we want to display the message only if the condition is falsey: