Skip to content

Instantly share code, notes, and snippets.

@vhbui02
Last active May 2, 2023 03:41
Show Gist options
  • Save vhbui02/378a01cd923b5035401c66c4c346cf58 to your computer and use it in GitHub Desktop.
Save vhbui02/378a01cd923b5035401c66c4c346cf58 to your computer and use it in GitHub Desktop.
[for command] perform a set of commands multiple times #fish #linux

local variable inside for body isn't exported to outside, remember to use set -x option.

Syntax

for <var-name> in $list_variable
	echo $<var-name>
end

Note: you could replace $list_variable with foo bar baz

Note 2: when declaring var-name, it's the name, not the variable substitution.

Common use cases

Traverse all files in a single/multiple directory

Single directory

  • for file in /path/to/directory
  • for file in /path/to/directory/

All directories inside a directory for dir in /path/to/directory/*/ (remember the trailing backspace, or it will print both files and dirs)

All files inside a directory for file in /path/to/directory/*

All files inside multiple directory for file in /path/to/directory/*/*

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