# Playing with arrays in zsh (or bash)

Alright Hashnode, what do you think of a quick shell lesson as a first blog post?

I use zsh as my main shell in linux, extended with oh-my-zsh, and I wanted to share a bit of my zsh configuration file, called a .zshrc file. I use this little bit to get a random emoji assigned as my terminal prompt 
```zsh
animal_array=(🤑👹☠️🙈🐧🐙)
animal_index=$(($RANDOM % ${#animal_array[@]}))
animal_emoji=${animal_array[$animal_index+1]}
SPACESHIP_CHAR_PREFIX=$animal_emoji
```
To summarize shortly, I'm defining a list of emojis, then randomly picking one of them and storing it in a variable.  Then I'm having my terminals theme use that emoji as the character prompt.

![Terminal random emojis](https://cdn.hashnode.com/res/hashnode/image/upload/v1627663020228/QvRXyj8y3.png)

I think this little code piece is a nice lesson on how arrays work in the shell, so lets go through and break this down to see what we are doing bit by bit.

## Defining the list  ![Writing a list ](https://developingthebusiness.com/wp-content/uploads/2017/04/Lista-Prospectos-1024x512.jpg) 
```zsh
animal_array=(🤑👹☠️🙈🐧🐙)
```
Alright here I am declaring an array with parentheses and populating it with 6 elements (or emojis in this case), each one separated with a *space* mind you, no commas in this language!
Then I assign the array to the variable "animal_array". Note that in zsh (and by extension bash which has almost 1:1 syntax) there must be no spaces around the equals sign.

## Getting our emoji ![Aiming at dart board](https://gametablereview.com/wp-content/uploads/2019/12/darts-300x250.png)
```zsh
animal_index=$(($RANDOM % ${#animal_array[@]}))
```
Now we want to pick an emoji from the list. We use `${}` around our array to print a value. In the brackets `[ ]` we put the index of the emoji we want. `@` says we want all elements. The `#` says we want the count of items.
`$RANDOM` is a bash function that returns an int between 0-32767.  The `%` is an interesting operand called 'modulo'. It returns the remainder of dividing `$RANDOM` by the count. So even though we may get a huge rand number, it will always be under the count and in bounds of our array
Then we use `$(( ))` around our expression to indicate that we will be performing a math expression, and that we expect to get a number. What we should now have is a random number between 0 and the length of our list, so we store it in animal_index. What this also means is that this code should work no matter how big our list is! Feel free to put as many emojis as you want.

## Storing that sumbitch ![woman opening a fridge at night](https://s11284.pcdn.co/wp-content/uploads/2019/07/woman-looking-into-refrigerator-700x0.jpg.optimal.jpg)
```zsh
animal_emoji=${animal_array[$animal_index+1]}
```
Finally, select an element in the array, saying we want the one at spot animal_index+1.
The reason for the +1 is because zsh is a 1-indexed language, which means arrays in zsh start counting at 1, as opposed to 0 like most languages, and in this case the number stored in `animal_index` will be between 0-5.
Now we have our random emoji, ready to insert into your terminal theme however you do. For my oh-my-zsh theme, I use spaceship prompt 
```zsh
SPACESHIP_CHAR_PREFIX=$animal_emoji
```
Well I hope you liked my first blog post! I think I may have to make another post describing what oh-my-zsh is and how to install it. 😉
Please, let me see what kind of configurations you all have in the comments. Or feel free to ask any questions or help understanding something. I look forward to getting to know you all more!
