If I use the 'cd' BASH built-in in a command substitution, it prints extra stuff to stdout, but only when piped to, eg., less.
$ echo `cd .`
# The output is a single newline, appended by echo.
$ echo `cd .` | less
# less displays:
ESC]2;my.hostname.com - tmp/testenv^G
(END)
What's going on there? This behavior isn't documented in the bash man page for cd. Obviously, running just 'cd' in a command substitution is silly, but something like
NEWDIR=`cd mypath; pwd`
could be useful.
I solved this by instead using
NEWVAR=`cd mypath > /dev/null 2>&1; pwd`
but I still want to know what's going on.
Bash Version: GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc.
Distro: Scientific Linux SL release 5.5 (Boron)
cd
output the real path. Or have you defined an alias forcd
?type cd
, result should becd is a shell builtin
. Make sureecho `builtin cd .` | less
results in the same. It forces use of the shell builtin command. Cannot repro, bash 3.2.48(1) on OS X.command
circumvents use of the bash builtin, but instead executes the program returned fromwhich cd
.