0

I am trying to pass a multiword argument in a function an echo the result in a simple fashion.

Currently I am doing this:

function myFunction { 
    multiWordString=""  
    for ((i=3; i<=$#; i++)); do 
            multiwordVariable+=" "${!i} 
    done 
    echo "multiwordVariable here => $multiwordSTRING"  
}

myFunction "$@" otherArgument1 otherArgument2 I am a multivariable element and yeah is rock

You can see there is several drawback in this approach, the insertion of multiple argument, the usage of a loop, the management of my argument positioning to retrieve my string of characters... that makes it a very approximating solution.

I would do something more simple, like just read my variable's string:

multiWordArgument="here an awesome multiword string"

function file_function { 
    echo $1 
}
myFunction $multiWordArgument

maybe someone know a way that is nearer to this process?

thanks

DiaJos
  • 493

1 Answers1

0

What is wrong with?:

function myFunction {     echo "$@";    }

myFunction "$@" Arg1 Arg2 I am a multivariable element and yeah is rock

Running that script will print:

$ ./script Hello World!

Hello World! Arg1 Arg2 I am a multivariable element and yeah is rock

Or, if you want to convert all the arguments to an string (separated by spaces):

IFS=$' \t\n' var=$*

Or, in some shells: var="$@" (not setting IFS).