Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

bash - Using a variable containing spaces as a single argument

Think you have a variable that contains a string of text with spaces inbetween and you want to use that as input arguments for another script. How would you go about passing the variable content without worrying about the space chars?

The following doesn't work:

VAR1=hello world
#... do something else
./a_script.sh $VAR1
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use double quotes:

VAR1=hello world
#... do something else
./a_script.sh "$VAR1"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...