Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

shell

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.

like image 406
karlosss Avatar asked Sep 21 '16 16:09

karlosss


People also ask

Is bin sh same as bash?

On most operating systems, sh is implemented by programs like dash, kash, and original Bourne Shell. sh is a Predecessor of bash. /bin/sh is an actual link to main implementations. It is a symlink in most POSIX systems. sh is not a programming language itself.

What is this #!/ Bin sh?

#!/bin/sh: It is used to execute the file using sh, which is a Bourne shell, or a compatible shell. #!/bin/csh: It is used to execute the file using csh, the C shell, or a compatible shell. #!/usr/bin/perl -T: It is used to execute using Perl with the option for taint checks.

Why is #!/ Bin bash needed?

#!/bin/bash Essentially it tells your terminal that when you run the script it should use bash to execute it. It can be vital since you may be using a different shell in your machine ( zsh , fish , sh , etc.), but you designed the script to work specifically with bash.


2 Answers

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

like image 57
bwt Avatar answered Nov 11 '22 12:11

bwt


I will assume, as is true on debian-ubuntu-like systems, that your /bin/sh is dash.

For dash, set the environment variable ENV to have the path to the initialization file of your choice. This would be dash's analog to ~/.bashrc.

If you are interested in login shells, dash reads ~/.profile.

As an example, one could add to ~/.profile:

ENV=$HOME/.shinit; export ENV

This would cause dash to read ~/.shinit when an interactive shell starts.

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

Those files are only read for login shells. If you just run sh at the command prompt, you are starting an interactive shell.

Documentation

From man dash:

Invocation
If no args are present and if the standard input of the shell is connected to a terminal (or if the -i flag is set), and the -c option is not present, the shell is considered an interactive shell. An interactive shell generally prompts before each command and handles programming and command errors differently (as described below). When first starting, the shell inspects argument 0, and if it begins with a dash ‘-’, the shell is also considered a login shell. This is normally done automatically by the system when the user first logs in. A login shell first reads commands from the files /etc/profile and .profile if they exist. If the environment variable ENV is set on entry to an interactive shell, or is set in the .profile of a login shell, the shell next reads commands from the file named in ENV. Therefore, a user should place commands that are to be executed only at login time in the .profile file, and commands that are executed for every interactive shell inside the ENV file. To set the ENV variable to some file, place the following line in your .profile of your home directory

       ENV=$HOME/.shinit; export ENV

substituting for “.shinit” any filename you wish.

If command line arguments besides the options have been specified, then the shell treats the first argument as the name of a file from which to read commands (a shell script), and the remaining arguments are set as the positional parameters of the shell ($1, $2, etc). Otherwise, the shell reads commands from its standard input. [Emphasis added.]

POSIX

From the POSIX standard (hat tip: chepner):

ENV
This variable, when and only when an interactive shell is invoked, shall be subjected to parameter expansion (see Parameter Expansion) by the shell, and the resulting value shall be used as a pathname of a file containing shell commands to execute in the current environment. The file need not be executable. If the expanded value of ENV is not an absolute pathname, the results are unspecified. ENV shall be ignored if the real and effective user IDs or real and effective group IDs of the process are different.

like image 25
John1024 Avatar answered Nov 11 '22 11:11

John1024