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 |
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 |
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. |
sh is still the default shell for FreeBSD users (not for root) and there is the .shrc file in user's home directory.
|
Hi,
Thanks for all the replies. This is how my .bashrc looks : Code:
alias quake='artsdsp -m .quake3/quake3' Code:
koodoo@knapsacker:~$ quake Code:
koodoo@knapsacker:~$ bash I think on my system sh is a link to the bash shell : Code:
koodoo@knapsacker:~$ ls -l /bin/sh 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. |
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 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. |
Quote:
|
Quote:
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". |
Confused
Hi,
I am confused :( and have a few questions. Quote:
Code:
koodoo@knapsacker:~$ bash Code:
When an interactive shell that is not a login shell is started, bash reads 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. |
Quote:
Quote:
Quote:
Until I've had a chance to re-read, I'll excuse myself from offering any other (possibly incorrect) information... |
Hey Dark_Helmet,
No harm done. Everybody knows your intentions. You were trying to help and it's all that matters. :) Thanks again ;) |
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. |
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. |
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. |
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. |