[LINUX] How to determine if a shell script was started in bash

How to determine if a shell script was started in bash

I want to detect that it is started using sh like sh ./check.sh and terminate it with an error.

script

check.sh

#!/bin/bash

if [ x$BASH_SOURCE = x"" ] ; then
    echo please run by $0 or bash $0 instead.
    exit 1
fi
echo OK: you are using bash.

Execution example

$ sh ./check.sh 
please run by ./check.sh or bash ./check.sh instead.
$ ./check.sh 
OK: you are using bash.
$ bash ./check.sh 
OK: you are using bash.

Recommended Posts

How to determine if a shell script was started in bash
[Ubuntu] How to execute a shell script
How to check if a value exists in an enum
How to pass arguments to a Python script in SPSS Modeler Batch
How to get a stacktrace in python
Creating a shell script to write a diary
Let's run a Bash script in Java
How to run a Maya Python script
How to study until a beginner in statistics gets started with Bayesian statistics
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to run some script regularly in Django
How to implement a gradient picker in Houdini
How to write a ShellScript Bash for statement
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
How to write a ShellScript bash case statement
How to pass the execution result of a shell command in a list in Python
How to specify a schema in Django's database settings
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
I made a script to put a snippet in README.md
A memorandum to run a python script in a bat file
How to use any or all to check if it is in a dictionary (Hash)
How to display DataFrame as a table in Markdown
What to do if a UnicodeDecodeError occurs in pip
How to create a simple TCP server / client script
How to execute a command using subprocess in Python
How to reference static files in a Django project
Generate a bash script to add Datadog monitor settings
[Linux] How to put your IP in a variable
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[python] How to check if the Key exists in the dictionary
How to slice a block multiple array from a multiple array in Python
How to import NoteBook as a module in Jupyter (IPython)
How to output a document in pdf format with Sphinx
[Linux] Copy data from Linux to Windows with a shell script
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
What to do if pip gives a DistributionError in Homebrew
Note 2 for embedding the scripting language in a bash script
[Python] How to write an if statement in one sentence.
How to temporarily implement a progress bar in a scripting language
How to define multiple variables in a python for statement
A note on how to load a virtual environment in PyCharm
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
I wrote a script to get a popular site in Japan
How to import NoteBook as a module in Jupyter (IPython)