./test.sh 1 2 '3 4'
./test.sh 1 2 '3 4'
On 01.12.2021 14:46, Stefan W wrote:
Hi! I'm writing very ordinary script. It processes some standard list of files being called with no arguments, and it handles passed files if
there are arguments. Nothing strange.
I'm know that correct iteration of list of arguments in bash could be simple as:
for i in "$@"; do
echo $i
done
[...]
But if I put in into variable, things got worse:
files="$@"
for i in $files; do
echo $i
done
[...]
I know that it is possible to use `if` and iterate with `for arg in
"$@";`. But I want to have only one universal algorithm for file processing, and just prepare a list of files and then pass it to function.
How could I have correct list of filenames (probably containing spaces)
in correctly iterable variable in bash?
Assigning to simple scalar variables make the program parameters flat,
that's hardly avoidable.
If you want to keep an existing array structure intact then use shell
arrays as in
files=( "$@" )
for i in "${files[@]}"; do
echo $i
done
Hi! I'm writing very ordinary script. It processes some standard list of files being called with no arguments, and it handles passed files if
there are arguments. Nothing strange.
I'm know that correct iteration of list of arguments in bash could be
simple as:
for i in "$@"; do
echo $i
done
[...]
But if I put in into variable, things got worse:
files="$@"
for i in $files; do
echo $i
done
[...]
I know that it is possible to use `if` and iterate with `for arg in
"$@";`. But I want to have only one universal algorithm for file
processing, and just prepare a list of files and then pass it to function.
How could I have correct list of filenames (probably containing spaces)
in correctly iterable variable in bash?
On Wed, 1 Dec 2021 15:42:29 +0100
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
On 01.12.2021 14:46, Stefan W wrote:
[...]
files=( "$@" )
for i in "${files[@]}"; do
echo $i
done
Surely that should be echo "$i"
On 01.12.2021 16:04, Spiros Bousbouras wrote:
On Wed, 1 Dec 2021 15:42:29 +0100
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
On 01.12.2021 14:46, Stefan W wrote:
[...]
files=( "$@" )
for i in "${files[@]}"; do
echo $i
done
Surely that should be echo "$i"I considered that just as test output and left it unchanged,
but surely you are right to better have a correctly quoted
paragon and quote it anyway, I agree. My fault.
Janis
On Wednesday, December 1, 2021 at 9:38:42 PM UTC, Janis Papanagnou wrote:
On 01.12.2021 16:04, Spiros Bousbouras wrote:
On Wed, 1 Dec 2021 15:42:29 +0100I considered that just as test output and left it unchanged,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
On 01.12.2021 14:46, Stefan W wrote:
[...]
files=( "$@" )
for i in "${files[@]}"; do
echo $i
done
Surely that should be echo "$i"
but surely you are right to better have a correctly quoted
paragon and quote it anyway, I agree. My fault.
Janis
here's a worked example .... - use quotes to avoid any potential shell expansion
for file in 1 2 3 '3 4'; do x="${file}";echo "<${x}>"; done
<3 4>
On 28.03.2022 00:42, glenn stevenson wrote:
here's a worked example .... - use quotes to avoid any potential shell expansion
for file in 1 2 3 '3 4'; do x="${file}";echo "<${x}>"; done
<1>
<2>
<3>
<3 4>
This is a bad example - what happens if you omit the quotes here?
$ for file in 1 2 3 '3 4' ; do x=${file} ; echo +${x}+ ; done
+1+
+2+
+3+
+3 4+
The variables ${file] and ${x} are still correctly expanded.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 443 |
Nodes: | 16 (2 / 14) |
Uptime: | 95:59:25 |
Calls: | 9,201 |
Files: | 13,480 |
Messages: | 6,055,132 |