Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[: not found - shell scripts no longer run well using sh, have to use bash?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
postcd
Posts: 133
Joined: 2022-01-08 18:33
Has thanked: 48 times
Been thanked: 2 times

[: not found - shell scripts no longer run well using sh, have to use bash?

#1 Post by postcd »

From Arch i am used to run scripts by "sh test"
yet i see some of them i have entitled #!/bin/bash
and on Arch it worked, but on Debian 11 i see it cause errors like below.

$ cat test

Code: Select all

#!/usr/bin/bash

if [[ "$1" == "" ]]; then echo "no parameter"; fi
sh test

Code: Select all

test: 3: [[: not found
bash test

Code: Select all

no parameter
can i cause any particular issues by setting alias:

Code: Select all

alias sh="/usr/bin/bash"
?

$ whereis sh bash

Code: Select all

sh: /usr/bin/sh /usr/share/man/man1/sh.1.gz
bash: /usr/bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz
Last edited by postcd on 2022-01-28 18:40, edited 1 time in total.

p.H
Global Moderator
Global Moderator
Posts: 3049
Joined: 2017-09-17 07:12
Has thanked: 5 times
Been thanked: 132 times

Re: [: not found - shell scripts no longer run well using sh, have to use bash?

#2 Post by p.H »

[[ ]] is a "bashism", not a standard shell syntax.
In Debian, for security and performance reasons, the default /bin/sh is dash, not bash. It can be changed to bash with

Code: Select all

dpkg-reconfigure dash
But IMO script designed for bash should have #!/bin/bash and be executed directly instead of passing them to sh.
And you should remove bashisms whenever possible like in the above example.

Code: Select all

if [ -z "$1" ]; then echo "no parameter"; fi

User avatar
canci
Global Moderator
Global Moderator
Posts: 2502
Joined: 2006-09-24 11:28
Has thanked: 136 times
Been thanked: 136 times

Re: [: not found - shell scripts no longer run well using sh, have to use bash?

#3 Post by canci »

There are guides on how to avoid Bashisms

https://mywiki.wooledge.org/Bashism
Image Stable / Asus VivoBook X421DA / AMD Ryzen 7 3700U / Radeon Vega Mobile Gfx (Picasso) / 8 GB RAM / 512GB NVMe

READ THIS:

* How to Post a Thread Here
* Other Tips and Great Resources

User avatar
Bloom
df -h | grep > 90TiB
df -h | grep > 90TiB
Posts: 504
Joined: 2017-11-11 12:23
Been thanked: 26 times

Re: [: not found - shell scripts no longer run well using sh, have to use bash?

#4 Post by Bloom »

Start your script with

Code: Select all

./test
and not with sh test.

Post Reply