[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] avoiding shell variable expansion
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] avoiding shell variable expansion |
Date: |
Fri, 4 Oct 2019 10:02:03 +0100 |
User-agent: |
NeoMutt/20171215 |
2019-10-03 19:47:42 +0000, Greg Silverman:
> In Python one can spawn a child process and avoid bash expanding command
> line arguments, e.g.
>
> //file: ls.py
> import subprocess
> proc = subprocess.Popen(['/bin/ls','*'],shell=False)
>
> then
> ./ls.py
> /bin/ls: cannot access '*': No such file or directory
>
> As the shell argument is set to False, the ls command is not
> passed to bash before being executed and the star is not
> expanded to ${PWD}.
[...]
That last sentence betrays a potential confusion.
In the
ls *
shell code, the shell doesn't expand * to ${PWD} or to the path
of the current working directory.
In a shell simple command line, when a word contains an unquoted
*, or ? or [...], the shell triggers what we call "filename
generation". It expands the string (here "*") which is here
considered as a "wildcard pattern" to the sorted list of
non-hidden files that match that pattern as separate arguments
So if the current directory contains a ".foo", "-a", "foo" and
"bar baz" entries, the shell expands "*" to "-a", "bar baz" and
"foo" arguments (as they are the non-hidden files and they all
match the "*" pattern since that pattern matches any number of
characters) which it passes to "ls".
"ls" is executed in the exact same way as if the shell code had
been:
\l\s -a 'bar baz' "foo"
(here demonstrating 3 different forms of quoting supported by
the "sh" syntax, though only the '...' are useful above as only
the SPC character is special in the shell syntax and needs
quoted there)
If you want ls to list the contents of the current directory,
you would run "ls" without argument at all or "ls" with a "."
argument which is exactly the same. You could run "ls" with
"--" and the contents of the $PWD shell variable as arguments,
but that would be less reliable as any component in the path to
the current working directory could have been renamed since the
last time the $PWD variable was computed.
For * to expand to "." only, the only way I can think of that
could happen is if your sh is based on ksh93 (like the /bin/sh
of Solaris 11) and the FIGNORE environment variable contains
!(.)
$ uname -rs
SunOS 5.11
$ FIGNORE='!(.)' /bin/sh -c 'echo *'
.
The expansion of * could never be the same as that of $PWD as
the expansion of * can never contain a "/" character.
--
Stephane
- Re: [Help-bash] avoiding shell variable expansion, (continued)
- Re: [Help-bash] avoiding shell variable expansion, Dmitry Alexandrov, 2019/10/03
- Re: [Help-bash] avoiding shell variable expansion, Eli Schwartz, 2019/10/04
- Re: [Help-bash] avoiding shell variable expansion, Andy Chu, 2019/10/04
- Re: [Help-bash] avoiding shell variable expansion, Andy Chu, 2019/10/04
- Re: [Help-bash] avoiding shell variable expansion, Dmitry Alexandrov, 2019/10/04
- Re: [Help-bash] avoiding shell variable expansion, Andy Chu, 2019/10/04
- Re: [Help-bash] avoiding shell variable expansion, Eli Schwartz, 2019/10/04
Re: [Help-bash] avoiding shell variable expansion, Eli Schwartz, 2019/10/04
Re: [Help-bash] avoiding shell variable expansion, Andreas Kusalananda Kähäri, 2019/10/04
Re: [Help-bash] avoiding shell variable expansion, Stephane Chazelas, 2019/10/04
Re: [Help-bash] avoiding shell variable expansion,
Stephane Chazelas <=