Issue
I am using this image which has bash v4.3.48 and curl v7.56.1:
https://hub.docker.com/r/bizongroup/alpine-curl-bash/tags?page=1&ordering=last_updated
Inside the docker I write the following script:
email_dest="[email protected]}}"
suffix="@gmail.com"
dm_to=${email_dest%"$suffix"}
if [[ $email_dest == *"@users.noreply.github.com"* ]]
then
echo "Email address is no reply. Please fix your email preferences in Github"
elif [[ $email_dest == *$suffix* ]]
then
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello <@'"$dm_to"'>. '{{inputs.parameters.workflow_name}}' "}' https://hooks.slack.com/services/T01JNE5DXA7/B0246T84N75/hHDk7RUg2BWl2bYbPoN9r
else
echo "Email address is not of digibank domain!"
fi
If I run this script with bash command <script_name> it will work as expected (Run the curl command). But if I run it with sh command <script_name> it will not run the curl command:
/ # bash send-message.sh
ok/ #
/ # sh send-message.sh
Email address is not of digibank domain!
Any suggestion of what it could be? and what should be changed so it will work with sh?
Solution
That lies within the differences between bash
and sh
:
sh
is POSIX compliant, whereas bash
isn't (fully).
As a best practice you should always include a shebang:
#!/usr/bin/env bash
echo "this is going to run within bash"
With this you can now omit calling the script via bash myscript
and just call it with ./myscript
and it is always going to use bash (even if you are in a zsh
, sh
or whatever else).
However, if you truly want to have a script that runs with both sh
and bash
then you should rewrite your script to be plain sh
compliant (i.e. POSIX).
TL;DR
Any suggestion of what it could be? and what should be changed so it will work with sh?
In your script you are using bash
extensions such as [[
which is why it does not work with sh
.
Checkout the links I posted above for more differences and how you can "convert" your bash
script into a sh
script.
The following site has a great summary on what to change in order to get your bash
script working for dash
which is an implementation of sh
: http://mywiki.wooledge.org/Bashism
Furthermore, you can also check if any issues exist by using the following site: https://www.shellcheck.net/
Answered By - F1ko