I’m writing a Bash script where I need to pass a string containing spaces to a function in my Bash script.

For example:

#!/bin/bash

myFunction
{
    echo $1
    echo $2
    echo $3
}

myFunction "firstString" "second string with spaces" "thirdString"

When run, the output I’d expect is:

firstString
second string with spaces
thirdString

However, what’s actually output is:

firstString
second
string

Is there a way to pass a string with spaces as a single argument to a function in Bash?

8 Answers
8

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *