Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for ${@:2} syntax in variable assignment #214

Closed
DocSalvager opened this issue Aug 15, 2014 · 3 comments
Closed

Allow for ${@:2} syntax in variable assignment #214

DocSalvager opened this issue Aug 15, 2014 · 3 comments

Comments

@DocSalvager
Copy link

Nice tool! Plan to use it.

But my first experiment revealed it needs a bit of tweaking. Did not allow for the valid syntax in a working script as described below...

In the output below, local _help_text="${@:2}" concatenates the function's positional arguments 2 and greater into a string and assigns them to the variable $_help_text...

48      local _help_text="${@:2}"`
                           ^––SC2124 Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.
@dualbus
Copy link

dualbus commented Aug 15, 2014

Well, you indeed have to use ${:2}, more specifically, IFS=' '; echo
"${
:2}"

The fact that the @ variant works is just really because shell is a mess,
but technically, the @ variant fails in many contexts.

So, shellcheck is right, use * to concatenate an array, and @ to expand the
array to its elements.

On Thu, Aug 14, 2014 at 7:33 PM, DocSalvage notifications@github.com
wrote:

Nice tool! Plan to use it.

But my first experiment revealed it needs a bit of tweaking. Did not allow
for the valid syntax in a working script as described below...

In the output below, local _help_text="${@:2}" concatenates the
function's positional arguments 2 and greater into a string and assigns
them to the variable $_help_text...

48 local _help_text="${@:2}"`
^––SC2124 Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.


Reply to this email directly or view it on GitHub
#214.

@koalaman
Copy link
Owner

dualbus is right, this is a valid warning.

The behavior you're relying on is undefined and often associated with bugs. Scripts would be better off using the explicitly concatenating form.

@evandrocoan
Copy link

Related to:

  1. "$@" serves a purpose, no? #1148 "$@" serves a purpose, no?
  2. SC2048 is overly zealous #127 SC2048 is overly zealous

See also How to portability use "${@:2}"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants