Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 :
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.
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.
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.
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.
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.
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".
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 ?
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...
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 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.