LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   shell behaves differently in rl5 and rl3 (https://www.linuxquestions.org/questions/linux-general-1/shell-behaves-differently-in-rl5-and-rl3-233468/)

Warmduvet 09-21-2004 02:21 PM

shell behaves differently in rl5 and rl3
 
Hi I'm running rh9.0 and at the moment I am playing with sed. I have put a function in my .bash_profile so that it would be available and not have to be declared every session. However the function only runs in rl3 at the shell prompt
In rl5 typing in the function name and arguments just returns me to the prompt no error message nothing. In rl5 I'm using xterm and rh default terminal program.

any help appreciated.

WD

Tinkster 09-21-2004 02:54 PM

G'day mate :}

Create a .bashrc and source .bash_profile from there.

Alternatively, invoke as "xterm -ls" to make it a login
shell ... if you want to understand why, read
Code:

man bash
/INVOCATION



Cheers,
Tink

Warmduvet 09-21-2004 03:38 PM

thanks Tink, is there a way to set bash to start with "bash --login" so I dont have to type it in every time? same with xterm -ls.

Can I put "bash --login" in my .bashrc file?

WD

btmiller 09-21-2004 04:19 PM

You could do that, but I personally just redirect my .bash_profile to .bashrc -- the following are the contents of my .bash_profile:

Code:

if [ -r ~/.bashrc ]
then
  . ~/.bashrc
fi

and then I put all of my customized setup stuff into my .bashrc file. This way it gets run regardless of whether or not I'm invoking a login shell.

Warmduvet 09-21-2004 04:43 PM

Yeah I have something similar in my .bash_profile

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi

Tinkster 09-21-2004 05:02 PM

You need it the other way round mate, since
.bash_profile won't be pulled if it's not a login
shell which in your case it isn't.


If it's NOT a login-shell bash only evaluates
~/.bashrc



Cheers,
Tink

Warmduvet 09-21-2004 07:04 PM

slight problem, I put "bash --login" in my .bashrc file and now I can't login as root :eek:

nor can I su - root from other user any suggestions...
yes I know I shouldn't have been working in root :o

my .bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:$HOME/bob/bin
BASH_ENV=$HOME/.bashrc
USERNAME="root"

export USERNAME BASH_ENV PATH
function testfind ()
{
echo "first argument: $1"
echo "second argument: $2"
sed -n "/$1/p" $2
}

My .bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ls='ls --color=yes'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

bash --login



WD

Tinkster 09-21-2004 07:12 PM

Boot into single user mode and edit it from there ...

If that fails, boot of CD (rescue mode).

Other (possbile) work-around:

su -c "mv ~/.bashrc ~/.bashrc.bad"


Cheers,
Tink

Warmduvet 09-21-2004 07:15 PM

booting to rl3 doesnt work can't login as root

dont I need to be root to do the mv?

Tinkster 09-21-2004 07:17 PM

Just try it :) It's all one line!


I won't f*ck up root account just to verify that
it works ;}


Cheers,
Tink

Warmduvet 09-21-2004 07:22 PM

LOL

OK it worked:D

still leaves me with ny original problem... and both .bashrc and .bash_profile

have

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi

so it shouldn't matter wether the shell is interactive or login

WD

Tinkster 09-21-2004 07:38 PM

OK, let's get that straight ...

:}


You need to source .bash_profile
from .bashrc for the modifications in
.bash_profile to be working in the xterm.

Sourcing .bashrc from both .bashrc and
.bash_profile won't give you any benefit.

If it's not a login shell .bash_profile won't
be pulled at all.


Cheers,
Tink

P.S.: I think I definitely deserve an affero after all this ;)

Warmduvet 09-21-2004 09:15 PM

OK affero done.

I understand what you mean by source .bash_profile and why,
I,m not sure how.
Do I replace...


in .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

with

# Source global definitions
if [ -f ~/.bash_profile ]; then
~ /.bash_profile
fi

???

WD

Tinkster 09-21-2004 10:07 PM

Got that right :)


Cheers,
Tink

Warmduvet 09-22-2004 12:09 AM

Ok I tried..

# Source global definitions
if [ -f ~/.bash_profile ]; then
. ~ /.bash_profile
fi

no luck, and login failed
and..

source ~/.bash_profile

no luck, and login failed
and..

ln -s /home/bob/.bashrc /home/bob/.bash_profile

this gave message ln : `/home/bob/.bash_profile': file exists
but login didn't fail but also didn't achieve desired result.

I have done quite a bit of googling and this seems to have been aproblem
for a few people but the solution never seems to be arrived at :scratch:

WD

btmiller 09-22-2004 12:45 AM

A couple of points to (hopefully) clarify things:

. <file> tells bash to source a particular file. What this means is bash basically executes the contents of the other file (there are some complications regarding subshells and the like that sourcing, as opposed to just plain executing, handles, but let's not worry about them at present).

The objective here is to have one set of common set-up commands that get executed for both login and nonlogin shells. For this to happen, we need to put the commands into one file. In my setup, I put all my personal set-up commands in .bashrc. But this is a problem, since then they aren't invoked in login shells. So I add the . ~/.bashrc to my .bash_profile to make .bash_profile run .bashrc and that way, .bashrc gets sourced regardless of whether I'm running a login or nonlogin shell. You could also do it the other way around and have .bashrc source your .bash_profile (where your custom setup commands would be located). Either way will work.

So, to summarize:

-- put your custom setup commands in either .bashrc or .bash_profile
-- have the other file source the file you put your commands into using . <filename>

If this does not work, post the exact errors that you get. Hopefully this helps a little and explains a bit of what's going on.

Warmduvet 09-22-2004 01:17 AM

Thanks for your reply I understood the what and why but have not been able to get the how to work.
Will try again maybe I missed something.

WD

Warmduvet 09-22-2004 01:36 AM

well putting

if [ -f ~/.bash_profile ]; then
. ~ /.bash_profile
fi

in my ~/.bashrc file didn't work.

after altering my user .bashrc as above I logout and try to login again I enter the user name hit enter, enter the password get the " your last login and date..." then "you have mail"
then instead of going to the user prompt I get returned to the original login prompt.

My setup stuff is in ~/.bash_profile which is why I am trying to source it from ~/.bashrc

WD

btmiller 09-22-2004 03:09 AM

Can you do 'ls -l ~/.bash_profile .bashrc' then ... if your .bash_profile is a symlink it will not be sourced (because of the if [ -f ~/.bash_profile ] test). This seems unlikely, but worth a check. You can also put an echo statement into each file to see if/when it executes, e.g. in your .bashrc put:

echo .bashrc is running...

Warmduvet 09-22-2004 04:11 AM

ls -l ~/.bash_profile returns the expected listing

this is my .bashrc file with 2 echos
when attempting to login the first echo "bashrc1 is running" repeats like
an endless loop until it kicks out back to the login prompt???
second echo " bashrc2 is running" doesnt appear at all.

# .bashrc

# User specific aliases and functions

echo "bashrc1 is running"

alias mntusb='sh mntusb.sh'
alias quit='sh quit.sh'
alias reup='sh reup.sh'

# Source global definitions
if [ -r ~/.bash_profile ]; then
. ~/.bash_profile
fi
echo " bashrc2 is running"

WD

Tinkster 09-22-2004 02:05 PM

Quote:

ls -l ~/.bash_profile returns the expected listing
What do you expect, and can we see it?

What's the content of .bash_profile, then?
There must be SOMETHING odd ...


Cheers,
Tink

Warmduvet 09-22-2004 03:33 PM

Its ok I figured it out :o .bashrc was sourcing .bash_profile and .bashprofile was sourcing .bashrc
so endless loop once I commented out the sourcing in .bash_profile everything works the way it should although.... the prompt changes from

[xxx@jekyll xxx]#
to
-bash-2.05b$ I can change this using the PS1 or 2 shell variables ?


WD

PS thanks for all the help Tink and btmiller

PPS you get up early Tink :D

Tinkster 09-22-2004 05:05 PM

Quote:

Originally posted by Warmduvet
Its ok I figured it out :o .bashrc was sourcing .bash_profile and .bashprofile was sourcing .bashrc
so endless loop once I commented out the sourcing in .bash_profile everything works the way it should although....
DOH :)

Quote:

the prompt changes from

[xxx@jekyll xxx]#
to
-bash-2.05b$ I can change this using the PS1 or 2 shell variables ?
Sure you can ... just check where they're being
re-set, too.

Quote:

PPS you get up early Tink :D
Indeed ... ~ 0520 ;)
But I'm home from work ~ 1620, too!


Cheers,
Tink


All times are GMT -5. The time now is 11:38 PM.