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.

I use bash terminals quite a lot in my daily work, and since I chose KDE for my desktop, I naturally use konsole for opening bash sessions. When I start konsole, the first session shows colored output, making it fast and easy to see what is a directory and what is a file on a ls listing, for example. When opening another tab (Ctrl+shift+T) or window (Ctrl+shift+N) though, bash output is not colored anymore. Any reason and quick workaround for that?

EDIT:

The distro I use is Slackware, in its slackware-current version (almost reaching version 14.0 by now). Based on the link supplied in the answer by @terdon, which shows the difference beetween .bash_profile and .bashrc, I compared the environment on the first session (redirected to file /tmp/win1) with the subsequent ones (redirected to files /tmp/win2 and /tmp/win3) and, while the difference among the latest is irrelevant:

$ diff /tmp/win2 /tmp/win3
24c24
< declare -x KONSOLE_DBUS_SESSION="/Sessions/7"
---
> declare -x KONSOLE_DBUS_SESSION="/Sessions/8"
47c47
< declare -x SHELL_SESSION_ID="7e9f9937fbfb4000a1ee37ddb8426069"
---
> declare -x SHELL_SESSION_ID="3a4ce4adac174000ba49a6ce8d381467"

the difference between the first and the second sessions' environment shows some hints:

$ diff /tmp/win1 /tmp/win2
2c2
< declare -x CPLUS_INCLUDE_PATH="/usr/lib/qt/include:/opt/kde3/lib/qt3/include:/usr/lib/qt/include:/opt/kde3/lib/qt3/include"
---
> declare -x CPLUS_INCLUDE_PATH="/usr/lib/qt/include:/opt/kde3/lib/qt3/include"
24c24
< declare -x KONSOLE_DBUS_SESSION="/Sessions/6"
---
> declare -x KONSOLE_DBUS_SESSION="/Sessions/7"
31c31
< declare -x LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.7z=01;31:*.ace=01;31:*.ACE=01;31:*.arj=01;31:*.bz2=01;31:*.cpio=01;31:*.deb=01;31:*.dz=01;31:*.gz=01;31:*.jar=01;31:*.lzh=01;31:*.lzma=01;31:*.rar=01;31:*.RAR=01;31:*.rpm=01;31:*.rz=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tlz=01;31:*.trz=01;31:*.txz=01;31:*.tz=01;31:*.tz2=01;31:*.xz=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.aac=01;35:*.AAC=01;35:*.anx=01;35:*.asf=01;35:*.ASF=01;35:*.au=01;35:*.axa=01;35:*.axv=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.divx=01;35:*.DIVX=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2A=01;35:*.m2v=01;35:*.M2V=01;35:*.m4a=01;35:*.M4A=01;35:*.m4p=01;35:*.M4P=01;35:*.m4v=01;35:*.M4V=01;35:*.mid=01;35:*.midi=01;35:*.mka=01;35:*.mkv=01;35:*.MKV=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mp4=01;35:*.MP4=01;35:*.mp4v=01;35:*.mpc=01;35:*.MPC=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.nuv=01;35:*.oga=01;35:*.ogv=01;35:*.ogx=01;35:*.ogg=01;35:*.OGG=01;35:*.pbm=01;35:*.pgm=01;35:*.png=01;35:*.PNG=01;35:*.ppm=01;35:*.qt=01;35:*.ra=01;35:*.RA=01;35:*.ram=01;35:*.RAM=01;35:*.rm=01;35:*.RM=01;35:*.spx=01;35:*.svg=01;35:*.svgz=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.tiff=01;35:*.TIFF=01;35:*.vob=01;35:*.VOB=01;35:*.wav=01;35:*.WAV=01;35:*.wma=01;35:*.WMA=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xcf=01;35:*.xpm=01;35:*.xspf=01;35:*.xwd=01;35:*.XWD=01;35:*.xvid=01;35:"
---
> declare -x LS_COLORS=""
37c37
< declare -x PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/local/lib/pkgconfig"
---
> declare -x PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
47c47
< declare -x SHELL_SESSION_ID="98d5491a0f354000896dc496554b5bad"
---
> declare -x SHELL_SESSION_ID="7e9f9937fbfb4000a1ee37ddb8426069"
53a54
> declare -x TOOLS_HOME="/home/claudio/tools"
58c59
< declare -x XDG_CONFIG_DIRS="/etc/xdg:/etc/kde/xdg:/etc/kde/xdg"
---
> declare -x XDG_CONFIG_DIRS="/etc/xdg:/etc/kde/xdg"

Note the variable LS_COLORS is only set on the first session, but it is defined at system level on /etc/profile.d/coreutils-dircolors.sh (excerpt):

# Set up the LS_COLORS environment:
if [ -f $HOME/.dir_colors ]; then
  eval `/bin/dircolors -b $HOME/.dir_colors`
elif [ -f /etc/DIR_COLORS ]; then
  eval `/bin/dircolors -b /etc/DIR_COLORS`
else
  eval `/bin/dircolors -b`
fi

Also, the variable TOOLS_HOME, which is defined in my .bashrc file, doesn't show on the first session, but it does on the second and third. This leads me to believe that just /etc/profile.d files are being sourced to the first session and only .bashrc to the subsequent ones.

share|improve this question
    
I don't have this issue. Can you tell me whether your bash aliases are any different between the first and second tabs? –  Luke Aug 14 '12 at 15:39
    
That's the funny part... I don't have any color settings in ~/.bashrc or ~/.bash_profile. It just works on the first konsole window. –  Claudio Aug 14 '12 at 17:52

2 Answers 2

Sounds like you have a .bashrc and .bash_profile problem. My guess is that settings are being set in .bash_profile instead of .bashrc. As for the colors, I assume you mean the colors in the output of the ls command.

You probably have a line like this in your ~/.bash_profile file:

alias ls='ls --color=tty'

Try placing that line in your ~/.bashrc file instead. For more information on .bashrc and bash_profile, login and interactive shells have a look here.

EDIT: You say that only /etc/profile.d files are read in the first session. That is kinda what I expected. Your terminal seems to treat the first opened tab as a login prompt and the others as interactive prompts. An easy hack is to create a .profile file in your $HOME directory and call ~/.bashrc from there:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi
share|improve this answer
    
That works indeed and the link you supplied is interesting. But I'm still wondering why it works with colors on the first konsole window but not on the next ones started from the same konsole process. If either .bashrc and .bash_profile don't have any color settings, why the first one comes with colors? –  Claudio Aug 14 '12 at 17:55
    
Well, probably because .bash_profile is read once at some point, either the first time you open konsole or when you log in and its settings are applied to the first konsole instance. When you open a tab, you are opening an interactive shell which does not read .bash_profile but only .bashrc. –  terdon Aug 14 '12 at 18:02
    
Generally, .bash_profile is only executed for login shells; Konsole+bash shells will, by default, only execute .bashrc (although this does not disprove your answer at all). –  Luke Aug 14 '12 at 18:04
    
@Luke yeah, I know, that's the problem. It looks like settings that should be in .bashrc are actually in .bash_profile and, so, are getting ignored in non-login shells. –  terdon Aug 14 '12 at 18:24
    
Shouldn't this be the other way around? I mean, since .bashrc gets executed every time after the first one, but its the first one which gets it right by loading the scripts on /etc/profile.d, shouldn't .bashrc execute them instead? –  Claudio Aug 16 '12 at 18:30

I see a few possibilities:

  1. Your aliases are different between the first and second tabs
  2. Your LS_COLORS environment variable is different between the tabs
  3. Konsole is using two different profiles between its tabs (and the second has colors disabled)
  4. There is a bug in Konsole

The first three are the most likely and should be easy to check.. Also note that #3 could be the cause of #1 or #2, since each profile can start with a different shell command.

share|improve this answer

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.