Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s
* [Loop over a variable range of numbers](#loop-over-a-variable-range-of-numbers)
* [Loop over the contents of a file](#loop-over-the-contents-of-a-file)
* [Loop over files and directories](#loop-over-files-and-directories)
* [Filter the argument list](#filter-the-argument-list)
* [VARIABLES](#variables)
* [Name a variable based on another variable](#name-a-variable-based-on-another-variable)
* [ESCAPE SEQUENCES](#escape-sequences)
Expand Down Expand Up @@ -675,6 +676,31 @@ for dir in ~/Downloads/*/; do
done
```

# Filter the argument list

This is a general technique for looping over the argument list, dropping
any arguments for which some `<test>` fails.

``` shell
for arg do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean $@?

shift
<test> || continue
set -- "$@" "$arg"
done
```

**Example:**

Drop arguments that are not directories. For simple, one-liner
tests like this we can use `&&` instead of `|| continue`.

``` shell
for arg do
shift
[ -d "$arg" ] && set -- "$@" "$arg"
done
```

# VARIABLES

## Name and access a variable based on another variable
Expand Down