Skip to main content
Became Hot Network Question
add more details
Source Link

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Thanks!

Edit

Code snippet of my script:

while getopts "i:"a:c:d:f:g:h" arg; do
  case $arg in
    i) files=$OPTARG;;
    # ...
  esac
done

for f in $files; do file "$f"; done

With files without spaces, my script works. But I would like to run the script passing files with spaces as argument in one of these ways:

./script.sh -i "foo\ bar another\ file"
./script.sh -i foo\ bar another\ file
./script.sh -i "'foo bar' 'another file'"
./script.sh -i 'foo bar' 'another file'

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Thanks!

Edit

Code snippet of my script:

while getopts "i:" arg; do
  case $arg in
    i) files=$OPTARG;;
  esac
done

for f in $files; do file "$f"; done

With files without spaces, my script works. But I would like to run the script passing files with spaces as argument in one of these ways:

./script.sh -i "foo\ bar another\ file"
./script.sh -i foo\ bar another\ file
./script.sh -i "'foo bar' 'another file'"
./script.sh -i 'foo bar' 'another file'

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Thanks!

Edit

Code snippet of my script:

while getopts "i:a:c:d:f:g:h" arg; do
  case $arg in
    i) files=$OPTARG;;
    # ...
  esac
done

for f in $files; do file "$f"; done

With files without spaces, my script works. But I would like to run the script passing files with spaces as argument in one of these ways:

./script.sh -i "foo\ bar another\ file"
./script.sh -i foo\ bar another\ file
./script.sh -i "'foo bar' 'another file'"
./script.sh -i 'foo bar' 'another file'
showing all the relevant commands will make it easier for other users to suggest better approaches
Source Link

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Since the $files variable will be read as a command line argument, I need a way to accept files with space.Thanks!

Edit

Code snippet of my script:

while getopts "i:" arg; do
  case $arg in
    i) files=$OPTARG;;
  esac
done

for f in $files; do file "$f"; done

With files without spaces, my script works. But I would like to know if there is a way to do this without changingrun the $IFS variable.script passing files with spaces as argument in one of these ways:

Thanks!

./script.sh -i "foo\ bar another\ file"
./script.sh -i foo\ bar another\ file
./script.sh -i "'foo bar' 'another file'"
./script.sh -i 'foo bar' 'another file'

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Since the $files variable will be read as a command line argument, I need a way to accept files with space.

I would like to know if there is a way to do this without changing the $IFS variable.

Thanks!

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Thanks!

Edit

Code snippet of my script:

while getopts "i:" arg; do
  case $arg in
    i) files=$OPTARG;;
  esac
done

for f in $files; do file "$f"; done

With files without spaces, my script works. But I would like to run the script passing files with spaces as argument in one of these ways:

./script.sh -i "foo\ bar another\ file"
./script.sh -i foo\ bar another\ file
./script.sh -i "'foo bar' 'another file'"
./script.sh -i 'foo bar' 'another file'
Source Link

Bash for loop with string var containing spaces

In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.

The following script works:

for f in foo\ bar another\ file; do file "$f"; done

This script also works:

for f in 'foo bar' 'another file'; do file "$f"; done

But the following script doesn't work:

files="foo\ bar another\ file"
for f in $files; do file "$f"; done

Not even this script works:

files="'foo bar' 'another file'"
for f in $files; do file "$f"; done

But, if the files do not contain space, the script works:

files="file1 file2"
for f in $files; do file "$f"; done

Since the $files variable will be read as a command line argument, I need a way to accept files with space.

I would like to know if there is a way to do this without changing the $IFS variable.

Thanks!