You are not running your script with bash
. You are running your script under dash
shell. The behavior does not happen on bash
- in bash
the command local
is very specially handled like ex. export
and arguments have same semantics as on assignment. Most probably the shebang of your script is #!/bin/sh
and sh
is linked to dash
on your system. Use shebang with bash
to run bash
.
local string=$1
is expanding $1
so it becomes:
local string=Catch a string my friend!
which creates a variable string
with value Catch
, empty variables a
string
and my
and friend!
is invalid variable value.
As always, quote variable expansion.
local string="$1"
Research when to quote variables in shell. Check your scripts with http://shellcheck.net
Side note: the !
in "something!"
triggers history expansion in bash. In bash when in interactive shell with history expansion enabled, you would put it ex. in single quotes "something"'!'
.