How should I handle the shebang when writing my own shell? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How should I handle the shebang when writing my own shell? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m writing a script to test a shell project to see that my custom shell has correct output.

str="HELLO"
echo $str
echo "*** YOU SHOULD SEE HELLO ABOVE ***"
ls *
echo "*** YOU SHOULD SEE THE OUTPUT FROM ls * ABOVE ***"
who|awk '{print $1}'
echo "*** YOU SHOULD SEE THE OUTPUT FROM who ABOVE ***"
echo $((1+2*3-4/5+6*7-8/9)))
echo "*** YOU SHOULD SEE THE NUMBER 49 ABOVE ***"

This is the output from the script

$ ./shell < ../tst_exp.sh 
'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin.
HELLO
*** YOU SHOULD SEE HELLO ABOVE ***
CMakeCache.txt       hello_2.7-0ubuntu1_amd64.changes  hello_2.7-0ubuntu1.diff.gz  hello_2.7.orig.tar.gz  jeff      not  script.sh  testresult.txt
cmake_install.cmake  hello_2.7-0ubuntu1_amd64.deb      hello_2.7-0ubuntu1.dsc      hello-2.7.tar.gz   Makefile  osh  shell

build-area:
hello_2.7-0ubuntu1_amd64.build    hello_2.7-0ubuntu1_amd64.deb  hello_2.7-0ubuntu1.dsc
hello_2.7-0ubuntu1_amd64.changes  hello_2.7-0ubuntu1.diff.gz    hello_2.7.orig.tar.gz

.bzr:
branch  branch-format  branch-lock  checkout  README  repository

CMakeFiles:
3.5.1          CMakeDirectoryInformation.cmake  CMakeRuleHashes.txt  feature_tests.bin  feature_tests.cxx  Makefile.cmake  shell.dir    TargetDirectories.txt
cmake.check_cache  CMakeOutput.log          CMakeTmp         feature_tests.c    Makefile2          progress.marks  shellparser.dir

hello:
ABOUT-NLS   AUTHORS    ChangeLog    config.in  configure.ac  COPYING  doc     INSTALL      Makefile.in  NEWS  README  tests   TODO
aclocal.m4  build-aux  ChangeLog.O  configure  contrib       debian   gnulib  Makefile.am  man      po    src     THANKS

hello-2.7:
ABOUT-NLS   AUTHORS    ChangeLog    config.h   config.log     configure     contrib  doc     INSTALL   Makefile.am  man   osh  README  stamp-h1  THANKS
aclocal.m4  build-aux  ChangeLog.O  config.in  config.status  configure.ac  COPYING  gnulib  Makefile  Makefile.in  NEWS  po   src     tests     TODO
*** YOU SHOULD SEE THE OUTPUT FROM ls * ABOVE ***
[21420] 
dac
dac
*** YOU SHOULD SEE THE OUTPUT FROM who ABOVE ***
49
*** YOU SHOULD SEE THE NUMBER 49 ABOVE ***

The output is expected, but if I add a shebang, which is recommended, then strange things happen. This is my own shell that I’m testing, so what should the shebang be for scripts that should be run with my shell, that I named osh and can put in /usr/local/bin or install like another shell?

I suppose the shebang should not be #!/usr/bin/env bash which is what my editor (CLion) recommends, and it can’t be #!/usr/bin/env osh because that is not installed and gives strange results.

How should I handle the shebang when writing my own shell?

Answer

It should be

#!/usr/local/bin/osh

if your shell is in /usr/local/bin. If /usr/local/bin is on your PATH then

#!/usr/bin/env osh

should work too… (In fact that’s the only point of env here — it will find osh wherever it’s installed, as long as it’s on the PATH, so it doesn’t matter if it’s in /usr/local/bin, /usr/bin etc. The first one found wins if multiple copies are installed.)

If you use the env variant, just make sure there isn’t another osh binary on your PATH: as schily points out, the Thompson shell could be installed as osh (although that strikes me as rather unlikely, but since you’re researching shells it might happen), and there’s also the OCaml build system’s debugging shell (typically in an omake package).

We are here to answer your question about How should I handle the shebang when writing my own shell? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji