Why does cd print when run in command substitution?
Posted
by
reasgt
on Super User
See other posts from Super User
or by reasgt
Published on 2012-08-29T19:39:07Z
Indexed on
2012/08/30
3:40 UTC
Read the original article
Hit count: 487
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)
© Super User or respective owner