You are not logged in.

#1 2022-10-06 18:16:23

zw963
Member
Registered: 2018-06-18
Posts: 200

Why built cd print folder name?

I saw many scripts write like this:

here=$(cd "$(dirname "$BASH_SOURCE")"; pwd)
source "$here/env.bash"

but, those code always not work on my arch linux, because the builtin cd output the folder name too.

 ╰─ $ builtin cd bin
/home/zw963/bin

 ╭─ 02:05  zw963 ⮀ ~/bin ⮀ ➦   0 
 ╰─ $ cd ..

 ╭─ 02:06  zw963 ⮀ ~ ⮀ ➦   0 
 ╰─ $ \cd bin
/home/zw963/bin

so, the above example will output

+ here='/home/zw963/bin
/home/zw963/bin'

instead of

here='/home/zw963/bin'

I have to change above code to following to fix it.

here=$(cd "$(dirname "$BASH_SOURCE")" >/dev/null; pwd)

But, i seem like this is not a general case, so, if there is a config to change is?

BTW: i set the $CDPATH variable in my ~/.bashrc

thank you.

Last edited by zw963 (2022-10-06 18:16:47)

Offline

#2 2022-10-06 19:38:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Why built cd print folder name?

I'm not sure what "many" scripts you see that in as it seems a bit odd to use a such a bash-specific oddity when `realpath $(dirname $0)` would get the same result more portably.

But in any case, something is wrong with your `cd` as it should definitely not output anything ... unless you pass '-' as a sole argument.  Oddly, your cd didn't output anything (which is correct) when you did the `cd ..`.  So what is the pattern?  Is it just when you cd into that bin directory that you get excess output?  Or is it only when you use the "builtin"?  In the latter case, what is your interactive cd aliased to?  Do you happen to have any shopt options set (e.g., cdspell)?

Last edited by Trilby (2022-10-06 19:48:13)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2022-10-07 08:45:34

zw963
Member
Registered: 2018-06-18
Posts: 200

Re: Why built cd print folder name?

I'm not sure what "many" scripts you see that in as it seems a bit odd to use a such a bash-specific oddity when `realpath $(dirname $0)` would get the same result more portably.

Yes, it use quite common, i know why, because not all linux distro (even, some arm device, e.g. router), can not guaranteed realpath or readlink is exists.....

Oddly, your cd didn't output anything (which is correct) when you did the `cd ..`.  So what is the pattern?

Sorry for confusing, the default `cd` in my laptop is a function.

Do you happen to have any shopt options set (e.g., cdspell)?

Yes, i enable it, but, after i disable it, still output folder name when `builtin cd bin`.
It not special for bin, but, it happen on any folder name except absolute path.

e.g.  following code not output the folder name.

 ╭─ 16:41  zw963 ⮀ ~ ⮀ ➦ ruby-3.1.2 
 ╰─ $ builtin cd /home/zw963/bin

 ╭─ 16:41  zw963 ⮀ ~/bin ⮀ ➦ ruby-3.1.2  0 
 ╰─ $ 

Following is my shopt config after i disable cdspell, and reboot my laptop, still print folder name,  any idea?

 ╰─ $ shopt 
autocd          off
assoc_expand_once       off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    on
cmdhist         on
compat31        off
compat32        off
compat40        off
compat41        off
compat42        off
compat43        off
compat44        off
complete_fullquote      on
direxpand       off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         on
extquote        on
failglob        off
force_fignore   on
globasciiranges on
globstar        off
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    off
huponexit       off
inherit_errexit off
interactive_comments    on
lastpipe        off
lithist         off
localvar_inherit        off
localvar_unset  off
login_shell     off
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
progcomp_alias  off
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off

---------------------

EDIT:

Okay, i know why, as i mention myself, i set the $CDPATH enviroment variable, if enable it, bash will tell you where cd into.

It not effect by the cdspell option.

so, can i disable it but still keeping $CDPATH feature?

Last edited by zw963 (2022-10-07 09:07:39)

Offline

Board footer

Powered by FluxBB