[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bash messes up spaces in command line agruments.
From: |
Dave B |
Subject: |
Re: Bash messes up spaces in command line agruments. |
Date: |
Wed, 07 May 2008 14:54:11 +0200 |
On Tuesday 6 May 2008 16:20, Herrmann, Justin wrote:
> Description: When I try to pass strings inside double or single quotes
> as command line arguments to my Bash script, leading spaces, trailing
> spaces, and multiple grouped embedded spaces are removed from the
> string. This also prevents me from passing in only a space or spaces as
> arguments to my script.
>
> Repeat-By: Save this script as 'startup':
>
> #!/bin/bash
> echo $#
> for ((index = 0; index <= $#; index++))
> do
> echo "$index |$(eval echo \${$index})|"
> done
> exit 0
>
> then type: ./startup ' some words '
>
> the script will print out:
> 1
> 0 |./startup|
> 1 |some words|
This is what I get:
$ ./startup ' some words '
1
0 |./startup|
1 | some words |
# ' some words '
Note that a space is missing. Try using the simpler indirection notation:
$ cat startup
#!/bin/bash
echo $#
for ((index = 0; index <= $#; index++)); do
echo "$index |${!index}|" # Try using this
echo "$index |$(eval echo \${$index})|"
done
exit 0
$ ./startup ' some words '
1
0 |./startup|
0 |./startup|
1 | some words |
1 | some words |
# ' some words '
I'm not sure about why, using your method, only a single space is lost
(instead of all leading, trailing and all but one intra-words).
I'm using
$ bash --version
GNU bash, version 3.2.33(1)-release (i686-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
--
D.