[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

shell script interpolation of variables containing spaces [was: afio batch operation]



On 11/6/22 05:01, Greg Wooledge wrote:
> On Sun, Nov 06, 2022 at 08:50:16PM +0800, jeremy ardley wrote:
>> On 6/11/22 19:38, Thomas Schmitt wrote:
>>> Then unpack the archive files:
>>>
>>>     for i in "$archive_dir"/*.afio.bz2
>>>     do
>>>       # One of:
>>>       #   afio -ivZ -P bzip2
>> Corrected:
>>
>> afio -ivZ -P bzip2 $i
>>
>>>       #   bunzip2 < "$i" | afio -iv -
>>>     done
>
> The "$i" needs to be "double-quoted", or else it will fail on files
> that have whitespace or globbing characters in their names.


+1


See below for a /bin/sh script that demonstrates shell interpolation of a variable containing spaces with no quotes, with double quotes, with single quotes, with nested double single quotes, and with nested single double quotes.


Shell interpolation of a variable containing shell globbing characters is left as an exercise for the reader (virtual machine clone advised).


David


2022-11-06 10:16:42 dpchrist@laalaa ~/sandbox/sh/debian-user/20221106-0501-greg-wooledge
$ cat /etc/debian_version ; uname -a
11.5
Linux laalaa 5.10.0-19-amd64 #1 SMP Debian 5.10.149-2 (2022-10-21) x86_64 GNU/Linux

2022-11-06 10:31:21 dpchrist@laalaa ~/sandbox/sh/debian-user/20221106-0501-greg-wooledge
$ cat script.sh
#!/bin/sh
# $Id: script.sh,v 1.6 2022/11/06 18:31:10 dpchrist Exp $

f='filename with spaces'

rm -f $f "$f" '$f' "'$f'" '"$f"'

echo "no quotes"
touch $f
ls -ldQ *

rm -f $f "$f" '$f' "'$f'" '"$f"'

echo "double quotes"
touch "$f"
ls -ldQ *

rm -f $f "$f" '$f' "'$f'" '"$f"'

echo "single quotes"
touch '$f'
ls -ldQ *

rm -f $f "$f" '$f' "'$f'" '"$f"'

echo "nested double single quotes"
touch "'$f'"
ls -ldQ *

rm -f $f "$f" '$f' "'$f'" '"$f"'

echo "nested single double quotes"
touch '"$f"'
ls -ldQ *

rm -f $f "$f" '$f' "'$f'" '"$f"'

2022-11-06 10:31:23 dpchrist@laalaa ~/sandbox/sh/debian-user/20221106-0501-greg-wooledge
$ /bin/sh script.sh
no quotes
drwxr-xr-x 2 dpchrist dpchrist 4096 Nov  6 10:31 "CVS"
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "filename"
-rwxr-xr-x 1 dpchrist dpchrist  539 Nov  6 10:31 "script.sh"
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "spaces"
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "with"
double quotes
drwxr-xr-x 2 dpchrist dpchrist 4096 Nov  6 10:31 "CVS"
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "filename with spaces"
-rwxr-xr-x 1 dpchrist dpchrist  539 Nov  6 10:31 "script.sh"
single quotes
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "$f"
drwxr-xr-x 2 dpchrist dpchrist 4096 Nov  6 10:31 "CVS"
-rwxr-xr-x 1 dpchrist dpchrist  539 Nov  6 10:31 "script.sh"
nested double single quotes
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "'filename with spaces'"
drwxr-xr-x 2 dpchrist dpchrist 4096 Nov  6 10:31 "CVS"
-rwxr-xr-x 1 dpchrist dpchrist  539 Nov  6 10:31 "script.sh"
nested single double quotes
-rw-r--r-- 1 dpchrist dpchrist    0 Nov  6 10:31 "\"$f\""
drwxr-xr-x 2 dpchrist dpchrist 4096 Nov  6 10:31 "CVS"
-rwxr-xr-x 1 dpchrist dpchrist  539 Nov  6 10:31 "script.sh"


Reply to: