From owner-freebsd-questions@FreeBSD.ORG Thu May 15 16:41:27 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 981731065671 for ; Thu, 15 May 2008 16:41:27 +0000 (UTC) (envelope-from lreid@cs.okstate.edu) Received: from a.cs.okstate.edu (a.cs.okstate.edu [139.78.113.1]) by mx1.freebsd.org (Postfix) with ESMTP id 5F1AE8FC1C for ; Thu, 15 May 2008 16:41:27 +0000 (UTC) (envelope-from lreid@cs.okstate.edu) Received: from [127.0.0.1] (localhost [127.0.0.1]) by a.cs.okstate.edu (Postfix) with ESMTP id C2B28154CF2 for ; Thu, 15 May 2008 11:41:26 -0500 (CDT) Message-ID: <482C67B4.1060603@cs.okstate.edu> Date: Thu, 15 May 2008 11:41:24 -0500 From: Reid Linnemann User-Agent: Thunderbird 2.0.0.6 (X11/20070926) MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <1210810823.5782.1253224263@webmail.messagingengine.com> <482C4CC3.3080802@cs.okstate.edu> <20080515110339.297fa37e@scorpio> In-Reply-To: <20080515110339.297fa37e@scorpio> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: Configuring Bash X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 May 2008 16:41:27 -0000 Written by Gerard on 05/15/08 10:03>> > On Thu, 15 May 2008 09:46:27 -0500 > Reid Linnemann wrote: > >> Written by Montag on 05/14/08 19:20>> >>> This should be a fairly simple process, I don't really know what I >>> am missing. >>> >>> I've got the following in the .bash_profile of a basic user account: >>> >>> # set prompt [user@host--/dir] $ (# for root) >>> PS1 = ' [\u@\h--\w] ' >>> case `id -u` in >>> 0) PS1='${PS1} # ';; # root >>> *) PS1='${PS1} $ ';; # everyone else >>> >>> When I log in, I am greeted with: >>> ${PS1} $ $ >>> >>> However, if I su to root, I get: >>> [root@host-- /home/user]# >>> >>> That is what I wanted, but for some reason it is not working for a >>> normal user. I thought perhaps the problem could be >>> that .bash_profile is only loaded when a non-login shell is >>> spawned, but a quick consultation of man bash revealed that bash >>> reads ~/.bash_profile when it is invoked as a login shell. >>> >>> My next thought was that it was a permissions issue, but: >>> su >>> chmod 777 .bash_profile >>> exit >>> logout >>> login >>> >>> That did not change the results, the output was still the same as >>> above. This is all being done at the console, by the way. >>> >>> Appreciate any advice, >>> >>> montag >>> -------------------------- >>> "Give the people contests they win by remembering the words to more >>> popular songs or the names of state capitals or how much corn Iowa >>> grew last year. Cram them full of non-combustible data, chock them >>> so damned full of 'facts' they feel stuffed, but absolutely >>> 'brilliant' with information. Then they'll feel they're thinking, >>> they'll get a sense of motion without moving." >> There are a few problems with what you are attempting here. >> >> Your ~/.bash_profile is executed once, only when you log in. When you >> su to root, a shell is started for root (according to root's shell >> set in /etc/passwd) and that shell will do whatever it wants to do as >> far as dotfile processing is concerned. Your ordinary >> user's .bash_profile is ignored. >> >> Since the ordinary user's .bash_profile is only executed once, when >> the user's shell starts, the *) condition is always met in the case >> statement, so that expression is executed: >> >> PS1='${PS1} $ ';; >> >> This will always result in PS1 being the literal '${PS1} $ ' for that >> user. Why? Because if you read your bash manual you'll see that >> variable expansion does not happen in single quoted strings. >> >> PS1="${PS1} \$ ";; >> >> The above string will do what you intend, it will set PS1 to whatever >> ${PS1} is expanded to, plus the extra ' $ ' (you have to escape a >> literal $ in a double quoted string). >> >> As has been mentioned before, what you really want is to use the '\$' >> literal to clue in the sh/bash to use a # for root and $ for all other >> users. >> >> PS1="${PS1} \\$ ";; >> >> That is the PS1 that will do it. >> >> But again, because su invokes a new shell, if root's shell is not a sh >> variant that uses $PS1, like the default csh, your prompt will not >> carry over. csh will uses its own internal prompt variable and ignore >> sh's PS1 environment variable. > > I placed the following in my ~/.bash_profile file. > > # This is the .bash_profile file > # Read on bash login and similar to .profile > # This file passes control to the '.bashrc' file if it is present > > if [ -f ~/.bashrc ]; then > . ~/.bashrc > fi > > Then in my ~/.bashrc file, I created an alias: > > alias su='su -m' > > Now, whenever I go to root, the environment is not modified and I still > have bash as my shell. I don't know if this will work for you or not. > It should not hurt to try it. > > > Nice, I missed that flag for su. I'll take advantage of that for certain.