Skip to main content
added 27 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace or any other character that is special in the syntax of the shell such as ;, |, &, * and many more.

Double-quoting is also required around expansions in list contexts at least (like "${files[@]}" and "$f" here) for file names containing globbing characters (*, ?, [, and \ in some versions of bash, and more if extglob is enabled) or characters of $IFS (space, tab and newline by default).

It's optional (but I'd recommend it) for file names that contain none of those and no non-ASCII character.

If the list of files comes from the current working directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name, though you'd want shopt -s nullglob beforehand to avoid getting a literal *f* if there's no match. (But then you could probably just use for f in *f*; do...done and avoid the array entirely).

The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays) or just info bash arrays.

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace or any other character that is special in the syntax of the shell such as ;, |, &, * and many more.

Double-quoting is also required around expansions in list contexts at least (like "${files[@]}" and "$f" here) for file names containing globbing characters (*, ?, [, and \ in some versions of bash, and more if extglob is enabled) or characters of $IFS (space, tab and newline by default).

It's optional (but I'd recommend it) for file names that contain none of those.

If the list of files comes from the current working directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name, though you'd want shopt -s nullglob beforehand to avoid getting a literal *f* if there's no match. (But then you could probably just use for f in *f*; do...done and avoid the array entirely).

The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays) or just info bash arrays.

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace or any other character that is special in the syntax of the shell such as ;, |, &, * and many more.

Double-quoting is also required around expansions in list contexts at least (like "${files[@]}" and "$f" here) for file names containing globbing characters (*, ?, [, and \ in some versions of bash, and more if extglob is enabled) or characters of $IFS (space, tab and newline by default).

It's optional (but I'd recommend it) for file names that contain none of those and no non-ASCII character.

If the list of files comes from the current working directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name, though you'd want shopt -s nullglob beforehand to avoid getting a literal *f* if there's no match. (But then you could probably just use for f in *f*; do...done and avoid the array entirely).

The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays) or just info bash arrays.

added 586 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace; it'swhitespace or any other character that is special in the syntax of the shell such as ;, |, &, * and many more.

Double-quoting is also required around expansions in list contexts at least (like "${files[@]}" and "$f" here) for file names containing globbing characters (*, ?, [, and \ in some versions of bash, and more if extglob is enabled) or characters of $IFS (space, tab and newline by default).

It's optional (but I'd recommend it) for plain file names that contain none of those. 

If the list of files comes from the current working directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name, though you'd want shopt -s nullglob beforehand to avoid getting a literal *f* if there's no match. (But then you could probably just use for f in *f*; do...done and avoid the array entirely.).

The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays) or just info bash arrays.

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace; it's optional (but I'd recommend it) for plain file names. If the list of files comes from the current directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name. (But then you could probably just use for f in *f*; do...done and avoid the array entirely.) The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays).

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace or any other character that is special in the syntax of the shell such as ;, |, &, * and many more.

Double-quoting is also required around expansions in list contexts at least (like "${files[@]}" and "$f" here) for file names containing globbing characters (*, ?, [, and \ in some versions of bash, and more if extglob is enabled) or characters of $IFS (space, tab and newline by default).

It's optional (but I'd recommend it) for file names that contain none of those. 

If the list of files comes from the current working directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name, though you'd want shopt -s nullglob beforehand to avoid getting a literal *f* if there's no match. (But then you could probably just use for f in *f*; do...done and avoid the array entirely).

The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays) or just info bash arrays.

POSIX file allows double-dash
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace; it's optional (but I'd recommend it) for plain file names. If the list of files comes from the current directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name. (But then you could probably just use for f in *f*; do...done and avoid the array entirely.) The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays).

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file "$f"; done

Quoting is required for file names containing whitespace; it's optional (but I'd recommend it) for plain file names. If the list of files comes from the current directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name. (But then you could probably just use for f in *f*; do...done and avoid the array entirely.)

Read more with man bash (search for Arrays).

If you're using bash you can use an array for this

#!/bin/bash
files=('foo bar' 'another file' file1 'file2')
for f in "${files[@]}"; do file -- "$f"; done

Quoting is required for file names containing whitespace; it's optional (but I'd recommend it) for plain file names. If the list of files comes from the current directory you can use wildcards as you'd expect, e.g. files=(*f*) to match any file or directory with f in its name. (But then you could probably just use for f in *f*; do...done and avoid the array entirely.) The -- marker for file tells it that any subsequent parameter is a filename - even if it starts with a dash.

Read more with man bash (search for Arrays).

Clarification and extension of description
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323
Loading
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323
Loading