in reply to Any tips on passing bash variables as arguments to a perl script (that contain spaces and/or non-ascii characters)

If I try to pass the variables directly to perl as arguments, (such as ovo-test.pl $OPC_MSG_TEXT), it does't store all the contents of $OPC_MSG_TEXT into $ARGV[0] -- it splits the contents based on spaces..my understanding is that this is default linux/unix behavior?

For practical purposes, yes. But to be completely correct, it is the shell which does the variable expansion and as you have said in the title in this case your shell is bash.

The variables are features of the shell and as such when you use them on the command line as arguments to other programs (perl or anything else) the shell substitutes the variables for their contents. As wrog points out, the simple way to tell the shell not to monkey with internal spacing when making this substitution is to enclose the variables in double quotes. You can test this pretty easily yourself if in any doubt:

$ dummy="What a lot of spaces." $ echo $dummy What a lot of spaces. $ echo "$dummy" What a lot of spaces.

It's not just spaces you need to worry about. Other characters are important to the shell and may result in unintended behaviour. See the section Quoting in the bash manpage for the full details and the bash hackers wiki for further discussion and examples.

  • Comment on Re: Any tips on passing bash variables as arguments to a perl script (that contain spaces and/or non-ascii characters)
  • Download Code