Skip to content

Instantly share code, notes, and snippets.

@moisoto
Last active November 26, 2019 21:35
Show Gist options
  • Save moisoto/7860bf24c49c24fadf02dcf4ac8d0c9f to your computer and use it in GitHub Desktop.
Save moisoto/7860bf24c49c24fadf02dcf4ac8d0c9f to your computer and use it in GitHub Desktop.
MacOS: ZSH Tidbits

Working with a word list on a for loop

On bash:

#!/bin/bash
LIST="one two three"
for item in $LIST ; do echo $item ; done

On zsh:

#!/bin/zsh
STRING="one two three"
LIST1=(${=STRING})
LIST2=(four five six)
for item in $LIST1 ; do echo $item ; done
for item in $LIST2 ; do echo $item ; done
for item (seven eight nine) ; do echo $item ; done

Portable but ugly:

LIST='one two three'
which -s setopt >/dev/null && setopt SH_WORD_SPLIT
for item in $LIST1 ; do echo $item ; done
which -s setopt >/dev/null && unsetopt SH_WORD_SPLIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment