0

I build a little bash script where I need to pass arguments but some of those arguments will have spaces because its a sentence or something similar. I don't want to store those arguments in a file because they get changed every time.

Can anybody provide an elegant solution for this. I'm a beginner and don't know much about bash!

CRM
  • 4,569
  • 4
  • 27
  • 33
cesr
  • 61
  • 8

2 Answers2

2

You need to quote your arguments such that your program receives a single argument.

Here is a simple example:

#!/bin/bash

echo "$1"
echo "$2"

If you invoke it like this:

./script.sh  'a b' 'c d'

The output will be:

a b
c d

Other posts that focus on this:

Why does my shell script choke on whitespace or other special characters? Passing arguments with spaces between (bash) script

CRM
  • 4,569
  • 4
  • 27
  • 33
1

Double quotes around the string you are passing as a variable will do the trick.

You can use environment variables by doing export VARIABLE = "the_value" and get the value by $VARIABLE

  • Hey I tried it and it gets recognized in the shell as one argument but not in the script. But thanks for the fast answer I am a bit closer where I want to be ! – cesr Apr 15 '20 at 08:01