-1

Realized that my bash script in development needs thourough testing from line 1, line by line

Started mate-terminal session

Split screen vertically, left side emacs editing bash script, right side watch command of said bash script, so that edits on the one side instantly become visible on the right side, all in 1 sight - the end of manually tideous switching from screen to screen, or workplace to workplace

bash script, variable block, defining associative array

*script not working, error message 'Syntax error: "(" unexpected' *

researching, discovering, that this error happens, if shell is incapable of working with associative arrays - sh can't do it, bash can do it

ending screen, back to pure bash, executing bash script, witnessing how it works fine

Question: Is there anything what can be done, so that screen command will rely on shell "bash" instead of "sh"? Any option causing that switch? --shell=bash? That's basically, what is needed.


UPDATE

Constantine A. B. is right. Turns out: It's technically necessary to put the shebang nowhere else, but in line 1 of a script, no matter what. Of course, right at the beginning of the line, making it the very first set of characters of the whole file. And likely basically set in line 1 alone, making everything else be in line 2 or later lines.

My mistake was, that I - of course - set the shebang for bash - but in Line 11, after a 10-line-comment-section, so, not in line 1.

Example:

BEFORE

emacs /tmp/test-script.sh (set executable, saved as follows)

# Super important stuff in need of being mentioned at the top
#!/bin/bash

declare -A asso_array=( [var1]="" [var2]="Seems to work" [var3]="Test" [var4]="" ) echo ${asso_array[@]}

echo "Hello World!"

watch /tmp/test-script.sh

Every 2.0s: /tmp/test-script.sh    Sat Jan 20 14:02:11 2024

/tmp/test-script.sh: 4: /tmp/test-script.sh: Syntax error: "(" unexpected

AFTER

emacs /tmp/test-script.sh (set executable, saved as follows)

#!/bin/bash
# Super important stuff in need of being mentioned at the top

declare -A asso_array=( [var1]="" [var2]="Seems to work" [var3]="Test" [var4]="" ) echo ${asso_array[@]}

echo "Hello World!"

Note: As soon as - and only then - you put the shebang in line 1, the path end-point - in this case "bash" - gets highlighted!

watch /tmp/test-script.sh

Every 2.0s: /tmp/test-script.sh    Sat Jan 20 14:05:11 2024

Test Seems to work Hello World!

So, lesson learned, re-cap:

shebang FIRST! NO MATTER WHAT!

It's a technical necessity for bash scripts.

Deviate, and the bash script basically is set to fail.

2 Answers2

4

You need to mark your script as bash script

That is done by starting it with the correct "shebang line", in this case

#! /bin/bash

This tells the system the script gets executed by bash, not by anything else, when it's run.

2

The currently accepted answer assumes the OP is trying to run a BASH script, which they are. However, the OP's question by itself asks about screen, so to answer the question by itself and not the OP's situation:

$ screen --help | grep SHELL
-s shell      Shell to execute rather than $SHELL.

E.g. $ screen -s bash -S myBashySessionName

Yoseph
  • 121