On Fri, Aug 16, 2024 at 18:59:15 +0200, freek--- via Bug reports for the GNU Bourne Again SHell wrote: > #!/bin/bash > init() { > [ -n "$@" ] && echo $@ > } > init $@
You have multiple errors in this script. The first error is that you used $@ without quotes, twice. If you want to preserve the argument list and pass it along without changing anything, you need to use "$@" with quotes. init "$@" The second error is that you're expanding "$@" in a context where a single string is expected. [ -n "$x" ] works, because x is a string variable, and it expands to exactly one word. "$@" on the other hand expands to any number of words. Zero or more. If you want to check whether you've got any parameters, "$@" is not what you want to check in the first place. Use $# instead. $# expands to the number of parameters. [ "$#" != 0 ] && echo "$*" The third error is that if you want to *print* the argument list, you most likely don't want echo "$@" or its unquoted variant. "$*" expands to a single string, with all of the arguments concatenated together, with a space (or the first character of IFS, if you've changed IFS) between arguments. However, this may not be what you want. Also, echo performs interpretation of the data that you pass it, which may *also* not be what you want. If you want to show the arguments in a human-readable way, you have to figure out how they should look. I'm personally fond of putting <angle brackets> around each argument. init() { if [ "$#" != 0 ]; then printf '<%s> ' "$@" echo fi } init "$@" That's one way to write this. Here's what you get when you call it: $ init 'argument one' '' arg3 arg4 <argument one> <> <arg3> <arg4>