0

I found that sh is a symbolic link to bash

lrwxrwxrwx 1 root root 4 May  9 15:23 /bin/sh -> bash

I tried to find a reason and i found out that bash is a implementation of sh and can behave like sh or POSIX. So scripts with #!/bin/sh are invoked by bash with a try to mimic behaviour of sh. And there are other impletations too like dash, csh etc. While reading this answer when invoked with /bin/sh bash moves to POSIX mode after reading startup files. I don't have much experience with shells but as these answers are stating : there is no sh by default in some systems and bash or any other shell is doing the homework for sh. And i want some expert opinion on this matter.

  1. When invoked with sh Bash enters POSIX mode after reading startup files. But what are these startup files?
  2. So the point is if bash is doing the work of sh in my system than does this means that there is no sh in my system and all the scripts with sh is invoked by bash or any other shell to which sh points to.
Zanchey
  • 1,312
tycoon
  • 150

2 Answers2

3

When invoked with sh Bash enters POSIX mode after reading startup files. But what are these startup files?

The startup files also depend on if it was started as sh or not.

  • In normal mode, as an interactive shell: /etc/profile and the first of ~/.bash_profile/ ~/.bash_login/~/.profile if it's a login shell, and just ~/.bashrc if not a login shell.

  • In sh mode: /etc/profile and ~/.profile if a login shell; and whatever the environment variable $ENV points to if interactive. In particular, it doesn't read the ~/.bash* files in sh mode.

See the manual for the actual details, it's not exactly simple: 6.2 Bash Startup Files

So the point is if bash is doing the work of sh in my system than does this means that there is no sh in my system and all the scripts with sh is invoked by bash or any other shell to which sh points to.

I'm not exactly sure what the question is, or what the other shell would be when Bash does the work of sh. But anyway, if /bin/sh points to /bin/bash (or is another copy of Bash), then all scripts using the #!/bin/sh hashbang, and commands started with e.g. the system() library call in C would run using Bash. But that's fine, since Bash is well capable of being compatible with those uses.

Also note that csh belongs to a different family, and is not compatible with POSIX sh.

ilkkachu
  • 138,973
2

On the original Unix systems, sh was a specific program with specific behavior. It was often contrasted with other shells which came along later, such as csh and ksh.

However, POSIX standardized the behavior of sh, and on modern open source operating systems, usually sh is actually some other implementation, and it merely has a mode which provides the POSIX behavior. For example, on FreeBSD, we'd refer to ash as sh; on OpenBSD, sh and ksh are the same program, and on Linux, you'll often find bash or dash as sh (although Debian permits many others, including zsh, mksh, posh, and potentially others).

In such a case, it's true that whatever shell is being invoked as sh is doing the work that sh would do because there's no independent sh program on those systems. It's merely a mode that provides the POSIX behavior.

In that POSIX mode, the startup files that are read are /etc/profile and $HOME/.profile for login shells and the contents of the file pointed to by $ENV for interactive shells, if that environment variable is not present. Shell-specific files are not invoked.

bk2204
  • 4,099
  • 7
  • 9