As you can see, the following two commands works as expected:
$ bash -c "cd \$1 && ls " -- ~/Desktop/
$ bash -c 'cd "$1" && ls' -- ~/Desktop/
But the following one doesn't:
$ bash -c "cd '$1' && ls " -- ~/Desktop/
Why does bash shell exhibit this behavior?
Regards,
HZ
On 04.12.2021 02:33, hongy...@gmail.com wrote:
As you can see, the following two commands works as expected:In my book, all work as expected.
$ bash -c "cd \$1 && ls " -- ~/Desktop/
$ bash -c 'cd "$1" && ls' -- ~/Desktop/
But the following one doesn't:
$ bash -c "cd '$1' && ls " -- ~/Desktop/
Why does bash shell exhibit this behavior?What behaviour; that $1 gets expanded on the outer shell level
[as expected]?
Because the expression passed to bash -c is double quoted and
thus variables like $1 will get expanded; the bash -c call
gets an expanded argument as parameter.
Why don't you try shell basics like shell quoting in simple
examples instead of trying to write complex shell programs?
It's so simple to check...
set wow
bash -c "echo '$1'"
bash -c "echo \$1"
bash -c 'echo "$1"'
Then what happens if you cd "" to an empty directory string?
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
What do you mean by saying ENOENT?
On Fri, 3 Dec 2021 19:33:50 -0800 (PST), "hongy...@gmail.com" <hongy...@gmail.com> wrote:
What do you mean by saying ENOENT?Install the script named "ddg" below in a directory in your
path, and chmod +x /path/to/ddg ,
then type at the shell prompt:
$ ddg ENOENT
----- start of script ddg -----
#!/usr/bin/perl
#
# quickly search duck duck go from command line
#
# From: Eli the Bearded <*@eli.users.panix.com>
# Newsgroups: comp.misc
# Date: Mon, 12 Apr 2021 17:46:51 +0000 (UTC)
use strict;
use warnings;
my $browser = 'lynx';
my @browser_args = qw(
-useragent=Lynx/2.8.9rel.1
-image_links
-noreferer
-accept_all_cookies
-cookie_save_file=/dev/null
);
my $query = join('+',@ARGV);
my $base = 'https://duckduckgo.com/lite/';
if(defined($query) and $query ne '') {
$base .= "?q=$query";
}
push(@browser_args, $base);
exec($browser, @browser_args);
----- end of script ddg -----
werner@X10DAi-00:~$ ls ~/Desktop/Desktop
Downloads.idm.wine "Newton's Principia for The Common
Reader.djvu"
File.com.qq.weixin.deepin work FileRecv.com.qq.im.deepin
霍金_时间简史.pdf
werner@X10DAi-00:~$ bash -c "cd '$1' && ls " -- ~/Desktop/
Mail PycharmProjects vmware Documents MusicREADME.md
Yozo_Officelog.txt Downloads News Templates Zoterogo
Pictures texmf hs_err_pid2195649.log Public VideosReader.djvu"
werner@X10DAi-00:~$ bash -c "cd \$1 && ls " -- ~/Desktop/
Downloads.idm.wine "Newton's Principia for The Common
File.com.qq.weixin.deepin work FileRecv.com.qq.im.deepinReader.djvu"
霍金_时间简史.pdf
werner@X10DAi-00:~$ bash -c 'cd "$1" && ls' -- ~/Desktop/
Downloads.idm.wine "Newton's Principia for The Common
File.com.qq.weixin.deepin work FileRecv.com.qq.im.deepin
霍金_时间简史.pdf
As you can see, the following two commands works as expected:
$ bash -c "cd \$1 && ls " -- ~/Desktop/
$ bash -c 'cd "$1" && ls' -- ~/Desktop/
But the following one doesn't:
$ bash -c "cd '$1' && ls " -- ~/Desktop/
Why does bash shell exhibit this behavior?
On 04.12.2021 04:33, hongy...@gmail.com wrote:
Then what happens if you cd "" to an empty directory string?
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
What do you mean by saying ENOENT?
It was a topical excursion to understand the 'cd' behavior in
the given application context.
By using various forms of quotes/escaped you had got your shell
variable expanded at different stages, effectively resulting in
getting a value or not. In the cases of not getting a value and
using it in context of the 'cd' command the question was how the
'cd' command would behave. Since you had used your command in a
conditional context cd "$arg" && some-cmd it's necessary to
know how cd "" would behave with an empty argument. Kornshell
would report an error ("cd: bad directory") and it seems Bash or
Zsh would not consider it an error but as a no-op. So I inspected
the POSIX specs, and it seemed to me that the given application
case would lead to and be reflected by the 'chdir()' Unix system
call; it should probably be passed back to the shell as an error.
For chdir(2) using an empty string argument the return is defined
as
[ENOENT]
A component of path does not name an existing directory or
path is an empty string.
Since shell's behaviors obviously seems to differ in case of an
empty cd argument the programmer has to take precaution to check
for empty arguments and handle that case appropriately. Or quote
correctly to avoid related bugs.
Janis
Then what happens if you cd "" to an empty directory string?
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
What do you mean by saying ENOENT?
On Saturday, December 4, 2021 at 10:17:58 AM UTC+8, Janis Papanagnou wrote:
Then what happens if you cd "" to an empty directory string?See my following testing:
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
werner@X10DAi-00:~$ cd ""
werner@X10DAi-00:~$
Then what happens if you cd "" to an empty directory string?
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
On Monday, December 6, 2021 at 9:23:13 AM UTC+8, hongy...@gmail.com wrote:
On Saturday, December 4, 2021 at 10:17:58 AM UTC+8, Janis Papanagnou wrote: >>> Then what happens if you cd "" to an empty directory string?
That seems to depend on the shell. I think it should fail withSee my following testing:
ENOENT (if I read POSIX correctly).
werner@X10DAi-00:~$ cd ""
werner@X10DAi-00:~$
werner@X10DAi-00:~$ echo $SHELL
/bin/bash
On 06.12.2021 02:27, hongy...@gmail.com wrote:
On Monday, December 6, 2021 at 9:23:13 AM UTC+8, hongy...@gmail.com wrote: >>> On Saturday, December 4, 2021 at 10:17:58 AM UTC+8, Janis Papanagnou wrote: >>>> Then what happens if you cd "" to an empty directory string?
That seems to depend on the shell. I think it should fail withSee my following testing:
ENOENT (if I read POSIX correctly).
Why shall I see it?
werner@X10DAi-00:~$ cd ""
werner@X10DAi-00:~$
werner@X10DAi-00:~$ echo $SHELL
/bin/bash
Yes, that's what I said in one of my posts here. Bash reports no error, despite cd'ing to an non-existing ('ENOENT') directory.
On Sun, 05 Dec 2021 20:50:40 -0500, Janis Papanagnou <janis_pa...@hotmail.com> wrote:
On 06.12.2021 02:27, hongy...@gmail.com wrote:
On Monday, December 6, 2021 at 9:23:13 AM UTC+8, hongy...@gmail.com wrote: >>> On Saturday, December 4, 2021 at 10:17:58 AM UTC+8, Janis Papanagnou wrote:
Then what happens if you cd "" to an empty directory string?See my following testing:
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
Why shall I see it?
werner@X10DAi-00:~$ cd ""
werner@X10DAi-00:~$
werner@X10DAi-00:~$ echo $SHELL
/bin/bash
Yes, that's what I said in one of my posts here. Bash reports no error, despite cd'ing to an non-existing ('ENOENT') directory.From "man bash"
cd [-L|[-P [-e]] [-@]] [dir]
Change the current directory to dir. if dir is not supplied, the value of the HOME shell variable is the default.
On Monday, December 6, 2021 at 10:48:57 AM UTC+8, David W. Hodgins wrote:
On Sun, 05 Dec 2021 20:50:40 -0500, Janis Papanagnou <janis_pa...@hotmail.com> wrote:
On 06.12.2021 02:27, hongy...@gmail.com wrote:From "man bash"
On Monday, December 6, 2021 at 9:23:13 AM UTC+8, hongy...@gmail.com wrote:
On Saturday, December 4, 2021 at 10:17:58 AM UTC+8, Janis Papanagnou wrote:
Then what happens if you cd "" to an empty directory string?See my following testing:
That seems to depend on the shell. I think it should fail with
ENOENT (if I read POSIX correctly).
Why shall I see it?
werner@X10DAi-00:~$ cd ""
werner@X10DAi-00:~$
werner@X10DAi-00:~$ echo $SHELL
/bin/bash
Yes, that's what I said in one of my posts here. Bash reports no error,
despite cd'ing to an non-existing ('ENOENT') directory.
cd [-L|[-P [-e]] [-@]] [dir]
Change the current directory to dir. if dir is not supplied, the value of the HOME shell variable is the default.
But if an empty string is supplied, NO-OP is the observed behavior for bash shell:
werner@X10DAi-00:~/Desktop$ pwd
/home/werner/Desktop
werner@X10DAi-00:~/Desktop$ cd ''
werner@X10DAi-00:~/Desktop$ pwd
/home/werner/Desktop
werner@X10DAi-00:~/Desktop$ cd ""
werner@X10DAi-00:~/Desktop$ pwd
/home/werner/Desktop
Ah. Correct. A null variable is not the same as no variable and causes the cd
command to become a nop.
echo $shbash
$sh -c 'pwd; CDPATH=/ cd ""'
echo
done
I agree that should be treated as an error rather then a nop.
Regards, Dave Hodgins
Ah. Correct. A null variable is not the same as no variable and causes
the cd
command to become a nop.
I agree that should be treated as an error rather then a nop.
On 06.12.2021 04:45, David W. Hodgins wrote:
Ah. Correct. A null variable is not the same as no variable and causes
the cd
command to become a nop.
I agree that should be treated as an error rather then a nop.
Well, the POSIX standard - after a very long procedural description
of what has to be done with directories in absolute or relative form,
given options -L and -P, given . and .., or with CDPATH specified or
not, and whatnot else - just specifies
"If directory is an empty string, the results are unspecified."
So I'd say that whatever outcome cd has given an empty string - whether
it appears to be sensible or inappropriate - is allowed by POSIX. (For
errors they say: "The working directory shall remain unchanged.")
Since files and directories have non-null names, specifying an empty
value I'd consider a programming-error that should be resulting in
an error that can be detected, caught and handled. (And, luckily, the
shell I practically prefer for many reasons does exactly that.)
The key (still) seems to be to specify a _non-empty_ directory argument
(by a correctly expanded and correctly quoted variable, if an argument
is provided that way).
Janis
All prominent shells don't change the directory when provided an empty string. So if you use a conditional like cd "" && cmd Bash and Zsh
will execute the cmd despite the cd had no effect. In your case of a
cd "$undef_var" that might not be a very helpful behavior, but it's on
you to fix it by taking some measure that undef_var will get _defined_.
I disagree; an empty string is a valid relative pathname. Shells above
that error out/change pwd to `//' are broken.
Janis Papanagnou <janis_papanagnou@hotmail.com> writes:
[...]
All prominent shells don't change the directory when provided an empty
string. So if you use a conditional like cd "" && cmd Bash and Zsh
will execute the cmd despite the cd had no effect. In your case of a
cd "$undef_var" that might not be a very helpful behavior, but it's on
you to fix it by taking some measure that undef_var will get _defined_.
csh, tcsh, and fish all report an error on "cd ''". (Not arguing
whether any of them are "prominent").
An interesting idea, but not one that's supported by POSIX.
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
Pathname Resolution:
A null pathname shall not be successfully resolved.
--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
I disagree; an empty string is a valid relative pathname. Shells
above that error out/change pwd to `//' are broken.
If "" (the empty pathname) ever should be a valid pathname, I would[...]
vote for it to be the root directory, not the current working
directory.
Helmut Waitzmann <nn.throttle@xoxy.net> writes:
[...]
If "" (the empty pathname) ever should be a valid pathname, I would[...]
vote for it to be the root directory, not the current working
directory.
But then it would be in effect an absolute pathname even though it
doesn't start with a '/' character.
Absolute pathnames, i. e. pathnames starting with the rootdirectory, always begin with an empty pathname component followed by
I can see that causing more problems.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 497 |
Nodes: | 16 (2 / 14) |
Uptime: | 25:04:57 |
Calls: | 9,793 |
Calls today: | 12 |
Files: | 13,749 |
Messages: | 6,188,097 |