How to preserve trailing whitespace in bash function arguments?

bash, function, whitespace

Solution

What happened to the trailing whitespace in the first argument?

The whitespace was included on the `echo` command line, but was discarded by the shell, the same as if you had typed:

echo -n Testing... 
                  ^
                  |----- there is a space here

How can preserve it?

Quote your variables:

function foo {
  echo -n "$1"
  echo "$2"
}

Problem

Consider the following bash script: ``` #!/bin/bash function foo { echo -n $1 echo $2 } foo 'Testing... ' 'OK' # => Testing...OK # Whitespace --^ ^ # Missing whitespace -----------------^ ``` What happened to the trailing whitespace in the first argument? How can preserve it?

Original source

Related problems