LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-24-2005, 05:20 PM   #1
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 33
aliases and the 'sh' shell


Hi,

Where does the 'sh' shell store it's list of aliases -- I mean in which file ?. I've used bash until now and in that I used the .bashrc file to store all my aliases. But it doesn't work with 'sh'. Also I know that sh is a link to the bash shell :


Code:
koodoo@knapsacker:~$ ls -l /bin/sh
lrwxrwxrwx  1 root root 4 2005-08-12 04:13 /bin/sh -> bash*
koodoo@knapsacker:~$
Thanks in anticipation.
 
Old 09-24-2005, 05:59 PM   #2
Stan the caddy
Member
 
Registered: Dec 2003
Location: Victoria, B.C
Distribution: Slackware
Posts: 61

Rep: Reputation: 15
There is no "sh" shell, it is a link pointing to your current shell. This can be useful for having a script be interpreted by whatever shell it was invoked with by adding
Code:
#!/bin/sh
to the head of your script. Since sh is not actually a shell it does not have such a file.
 
Old 09-24-2005, 06:28 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Actually, there is an "sh" shell. Bash and some others are later revisions to the original sh shell. On modern systems, yes, sh is linked to bash to maintain backward compatibility. It would be a mistake, though to just arbitrarily link sh to any other shell; the sh syntax is much different from the csh or tcsh syntax for example.

As for the OP's original question, I do not know what files sh originally relied on. You may need to do some research to find out. But I doubt any file with "bash" in the name will be included since sh is a precursor to bash. Maybe ".profile", ".login", and/or ".logout"...

When invoked as sh, bash should operate in backward compatibility mode. Worst csae, you can crack open the bash source code to examine what files it looks at in those cases.

Last edited by Dark_Helmet; 09-24-2005 at 06:31 PM.
 
Old 09-24-2005, 06:48 PM   #4
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
sh is still the default shell for FreeBSD users (not for root) and there is the .shrc file in user's home directory.
 
Old 09-24-2005, 10:38 PM   #5
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Hi,

Thanks for all the replies.

This is how my .bashrc looks :
Code:
alias quake='artsdsp -m .quake3/quake3'
I open up a terminal and type :

Code:
koodoo@knapsacker:~$ quake
-sh: quake: command not found
koodoo@knapsacker:~$
but if I do :

Code:
koodoo@knapsacker:~$ bash
koodoo@knapsacker:~$ quake
Q3 1.32b linux-i386 Nov 14 2002
----- FS_Startup -----
Current search path:
/home/koodoo/.q3a/baseq3

------------quake starts--------------------

I think on my system sh is a link to the bash shell :
Code:
koodoo@knapsacker:~$ ls -l /bin/sh
lrwxrwxrwx  1 root root 4 2005-08-12 04:13 /bin/sh -> bash*
koodoo@knapsacker:~$
but if that is the case, why does it then, not read the .bashrc file ?
Also, this is the default shell chosen by the system for any user.



Thanks Dark_Helmet for the tip. I copied the .bashrc file as .profile and it worked !
Actually I tried all of .shrc, .login, .logout, .profile out of which only the last one worked.

Thank you all again.

Last edited by koodoo; 09-24-2005 at 10:40 PM.
 
Old 09-25-2005, 12:27 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, there are a couple things at work here that confused the situation.

First and foremost, bash does not read the ~/.bashrc file by default. In fact, bash will never read it unless it's explicitly told to. If you look in your ~/.bash_profile, you're likely to see a few lines like this:
Code:
if [ -r ${HOME}/.bashrc ] ; then
  source ${HOME}/.bashrc
fi
That bit of text text tells bash to incorporate ~/.bashrc if it exists and is readable. Just to reiterate, bash will never read ~/.bashrc unless explicitly told to. The ~/.bashrc file is just a common convention.

Ok, so now the question becomes "why didn't bash read ~/.bash_profile, and consequently ~/.bashrc?" That has to do with how bash is invoked. There are differences in what files bash reads on startup if it is started as a "loging" or "non-login" shell. I won't claim to know all the ins-and-outs, but if bash is started and waits for the user to manually enter commands, then it's a login shell. If bash is invoked from a shell script, it's a non-login shell. You'll need to read the man page for bash to get more details; I may be confusing login shells versus interactive shells.

Lastly, I would be willing to bet this is a new alias: one you just created. As a general rule, if you make aliases or changes to your startup files for bash, it's best to logout of the system entirely (possibly even rebooting the machine) for the changes to take effect. You can manually source the changed files to get the changes included in the current shell, but as you encountered, new shells created by scripts or other xterms will not necessarily get the new changes. That also has something to do with how X windows handles a user's shell startup files.

To avoid confusion in the future, I suggest you remove ~/.profile, put the alias back in ~/.bashrc, make sure ~/.bash_profile reads ~/.bashrc (like the example above), reboot, and check to see if the alias works.

Last edited by Dark_Helmet; 09-25-2005 at 12:29 AM.
 
Old 09-25-2005, 02:06 PM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
There is no "sh" shell, it is a link pointing to your current shell.
Do you really believe this? ls -l shows that my FC2 /bin/sh is a symlink to /bin/bash. My current (and login) shell is /bin/ksh (the real one, not pdksh), so that gives the lie to your statement. On AIX all shells point to /bin/sh.
 
Old 09-25-2005, 02:15 PM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
To avoid confusion in the future, I suggest you remove ~/.profile, put the alias back in ~/.bashrc, make sure ~/.bash_profile reads ~/.bashrc (like the example above), reboot, and check to see if the alias works.
Doesn't POSIX require /bin/sh to source its configuration from .profile? Korn starts with .profile, where it's customary to include a source of .kshrc (analagous to /bin/sh's .shrc and bash's .bashrc). Make things nice and standard, only bash upsets the apple cart by having its own .bash_profile to keep it separate from bourne shells. Use the ENV variable to store the name of your .??rc file and put your aliases in there so they'll be sourced by each subshell you invoke.

BTW, a read of the bash man page reveals that "a [bash] shell invoked as sh does not attempt to read and execute commands from any other startup files" and "a non-interactive shell invoked with the name sh does not attempt to read any other startup files".
 
Old 09-26-2005, 02:34 PM   #9
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Question Confused

Hi,

I am confused and have a few questions.

Quote:
Just to reiterate, bash will never read ~/.bashrc unless explicitly told to. The ~/.bashrc file is just a common convention.
I noticed one thing, that I didn't have a .bash_profile (the file doesn't exit). But this works :

Code:
koodoo@knapsacker:~$ bash
koodoo@knapsacker:~$ quake
Q3 1.32b linux-i386 Nov 14 2002
----- FS_Startup -----
Current search path:
/home/koodoo/.q3a/baseq3

------------quake starts--------------------
From the manpage of bash :
Code:
When an interactive shell that is not a login shell is started, bash reads
       and  executes  commands  from ~/.bashrc, if that file exists.
So this explains why it's working.


a) Do I have a real 'sh' shell or the one I'm working on is bash invoked as sh or are these two the same ?

I created a ~/.profile file and put my alias there. The alias is working. From the bash's man page :

"When invoked as an interactive shell with the name sh, bash looks for the
variable ENV, expands its value if it is defined, and uses the expanded
value as the name of a file to read and execute. Since a shell invoked as
sh does not attempt to read and execute commands from any other startup
files, the --rcfile option has no effect. A non-interactive shell invoked
with the name sh does not attempt to read any other startup files. When
invoked as sh, bash enters posix mode after the startup files are read.

When bash is started in posix mode, as with the --posix command line
option, it follows the POSIX standard for startup files. In this mode,
interactive shells expand the ENV variable and commands are read and exe_
cuted from the file whose name is the expanded value. No other startup
files are read."

So I guess the better option would be to create a ENV variable and set it's value .shrc . Wouldn't it ?

Any suggestions ?

Thanks again.
 
Old 09-26-2005, 02:47 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
From the manpage of bash :

Quote:
When an interactive shell that is not a login shell is started, bash reads
and executes commands from ~/.bashrc, if that file exists.
That would be my mistake. My apologies. Disregard everything I said about bash not reading ~/.bashrc. I think I may have mixed up ~/.bashrc and /etc/bashrc. I'll have to review the documentation to get myself straightened out.

Quote:
a) Do I have a real 'sh' shell or the one I'm working on is bash invoked as sh or are these two the same ?
On your system, given the output of ls -l /bin/sh (above) you do not have the sh shell on your system. You have bash, which can be run in sh-compatible mode (by starting the shell as "sh" instead of "bash").

Quote:
Thanks again.
I'm afraid I may have done more harm than good
Until I've had a chance to re-read, I'll excuse myself from offering any other (possibly incorrect) information...
 
Old 09-26-2005, 03:11 PM   #11
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Hey Dark_Helmet,

No harm done. Everybody knows your intentions. You were trying to help and it's all that matters.

Thanks again
 
Old 09-26-2005, 03:54 PM   #12
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
I have one more question :

From the manpage :

a) When an interactive shell that is not a login shell is started, bash reads
and executes commands from ~/.bashrc, if that file exists.

b) When bash is invoked as an interactive login shell, or as a non-interac_
tive shell with the --login option, it first reads and executes commands
from the file /etc/profile, if that file exists. After reading that file,
it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that
order, and reads and executes commands from the first one that exists and
is readable.

c) When bash is started non-interactively, to run a shell script, for exam_
ple, it looks for the variable BASH_ENV in the environment, expands its
value if it appears there, and uses the expanded value as the name of a
file to read and execute. Bash behaves as if the following command were
executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file
name.

d) If bash is invoked with the name sh, it tries to mimic the startup behav_
ior of historical versions of sh as closely as possible, while conforming
to the POSIX standard as well.

i) When invoked as an interactive login
shell, or a non-interactive shell with the --login option, it first
attempts to read and execute commands from /etc/profile and ~/.profile, in
that order. The --noprofile option may be used to inhibit this behavior.

ii) When invoked as an interactive shell with the name sh, bash looks for the
variable ENV, expands its value if it is defined, and uses the expanded
value as the name of a file to read and execute.

iii) A non-interactive shell invoked
with the name sh does not attempt to read any other startup files. When
invoked as sh, bash enters posix mode after the startup files are read.
Slighlty Confusing !

When bash is started in posix mode, as with the --posix command line
option, it follows the POSIX standard for startup files. In this mode,
interactive shells expand the ENV variable and commands are read and exe_
cuted from the file whose name is the expanded value. No other startup
files are read.

Since in my case I have bash which is invoked as sh I guess case d applies. So, where do I add the ENV variable ? Where will bash always look for the ENV variable ?
A non-interactive shell called as sh would look for ENV in its environment (that of the parent shell, if it is called from one) Where will an interactive shell called as sh look for ENV ?
I guess It'd be okay to add ENV to /etc/profile

I may have confused a lot of things, but I'm still learning. Also, my interpretation of the man page might not be that good. Please bear with me.

Thanks.
 
Old 09-30-2005, 09:47 PM   #13
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Hey,

Thanks for everyone's help !
Since currently my problem of aliases is solved by creating a ~/.profile and putting the aliases there I guess I'll continue using it.

If there's a better way to it, I'll greatly appreciate any input/explainations that anyone would like to offer.

Thanks again.
 
Old 06-20-2006, 12:16 AM   #14
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
When you log in the first time, if /bin/sh is listed in "/etc/passwd" as your default shell, /etc/profile and ~/.profile are read. This is where you would define the $ENV and $BASH_ENV variables, and your path.
I do have a question for you. Why are you using the bourne shell as your default? Since on your system bash is the normal default, using bash would probably be easier.

Last edited by jschiwal; 06-20-2006 at 12:19 AM.
 
Old 08-05-2006, 04:19 AM   #15
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Original Poster
Rep: Reputation: 33
Hi Jschiwal,
thanks for the post and sorry for such a late reply.
Actually I was on a vacation and so couldn't check my mails and reply.

I've been away from computers for more than two months now. I'll try what you suggested and then post whatever I find.

Meanwhile... thanks for all the help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
Aliases SkyeFyre Fedora 8 03-18-2005 07:35 PM
See aliases gubak Linux - Newbie 0 08-04-2004 06:13 AM
execl: couldn't exec `/bin/sh' Daredevil Linux - Newbie 2 04-12-2004 11:06 AM
Comparison of shell functions and aliases? slakmagik Programming 3 02-07-2004 05:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration