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.
screen
does have options to tell which shell it will run e.g. when starting a new window, but that's a bit different than the question of what shell will be launched to run the script. That is, assuming you don't start the script directly from screen (which you could do), but from a shell session just running inside the screen (as you show in the edit). – ilkkachu Jan 20 '24 at 15:55