Richard
Richard

Reputation: 15592

How to pass command line argument with space in Bash scripts

Let's say there is one script s1, and I need to pass argument $1 with value foo bar, with space in it. This can be done

./s1 "foo bar"

however, when I want to run the above command in another script (say s2), how should I put it? If I put it as above, foo bar will be interpreted as two arguments (to s1) rather than one.

Upvotes: 9

Views: 20997

Answers (3)

phoxis
phoxis

Reputation: 61970

Use single quotes.

./script 'this is a line'

To consider variable substitutions use double quotes

./script "this is a line"

Upvotes: 1

kch
kch

Reputation: 1

How about:

./s1 foo\ bar

Would that work?

Upvotes: -4

cnicutar
cnicutar

Reputation: 182734

You can try quoting $1:

./s2 "$1"

Upvotes: 17

Related Questions