Search code examples
linuxshellsh

Is there an alternative for .bashrc for /bin/sh?


I need a script which is run on startup of /bin/sh, similar to .bashrc for /bin/bash. Is there any way to do this?

EDIT:

I tried both /etc/profile and ~/.profile, I wrote echo 'hello world' to both files. None of these are working. When I type sh into the console, nothing pops up.

I am using ArchLinux.


Solution

  • In Arch, /bin/sh is a symlink to /bin/bash, which has quite a few rules about startup scripts, with special cases when called sh :

    If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, ...

    If you start it from the console, without any command, i.e. as an interactive, non-login shell, you should use the ENV variable :

    export ENV=~/.profile
    sh
    

    or

    ENV=~/.profile sh 
    

    When invoked as an interactive [non login] 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.

    Alternatively you can use the --login option to make it behave like a login shell, and read the .profile file.

    sh --login
    

    When invoked as an interactive login shell [with the name sh], 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