Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)

share|improve this question
    
I cannot replicate the described behaviour. Do you use $CDPATH? It makes cd output the real path. Or have you defined an alias for cd? –  choroba Aug 29 '12 at 19:44
    
Run type cd, result should be cd is a shell builtin. Make sure echo `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. –  Daniel Beck Aug 29 '12 at 19:47
    
I think something must have changed since bash 3.2. See this thread: lists.gnu.org/archive/html/bug-bash/2007-04/msg00019.html The problem is that POSIX requires printing of the directory when CDPATH was used, even when not interactive. Why this changed in bash 4.x I don't know, but I can't reproduce it there either. –  reasgt Aug 29 '12 at 19:49
    
Use of command circumvents use of the bash builtin, but instead executes the program returned from which cd. –  Daniel Beck Aug 29 '12 at 19:52
add comment

1 Answer

up vote 0 down vote accepted

You are seeing the xterm escape sequence ( ESC 2; new-title^G ) to change the window title.

(The following is mostly conjecture.)

When you write output to the terminal, your terminal emulator (I'm guessing) removes it from the stream before displaying it on the screen. When you pipe the output through less, you see all the output.

Check the value of $PROMPT_COMMAND; you might see a command there that prints the string (likely involving $PWD or a call to pwd).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.