LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   aliases and the 'sh' shell (https://www.linuxquestions.org/questions/linux-newbie-8/aliases-and-the-sh-shell-366688/)

koodoo 09-24-2005 05:20 PM

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.

Stan the caddy 09-24-2005 05:59 PM

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.

Dark_Helmet 09-24-2005 06:28 PM

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.

Emerson 09-24-2005 06:48 PM

sh is still the default shell for FreeBSD users (not for root) and there is the .shrc file in user's home directory.

koodoo 09-24-2005 10:38 PM

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.

Dark_Helmet 09-25-2005 12:27 AM

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.

eddiebaby1023 09-25-2005 02:06 PM

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.

eddiebaby1023 09-25-2005 02:15 PM

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".

koodoo 09-26-2005 02:34 PM

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.

Dark_Helmet 09-26-2005 02:47 PM

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...

koodoo 09-26-2005 03:11 PM

Hey Dark_Helmet,

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

Thanks again ;)

koodoo 09-26-2005 03:54 PM

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.

koodoo 09-30-2005 09:47 PM

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.

jschiwal 06-20-2006 12:16 AM

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.

koodoo 08-05-2006 04:19 AM

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. :)


All times are GMT -5. The time now is 05:20 PM.