Joe Ainsworth
Joe Ainsworth

Reputation: 571

Execute a bash script from the command line which requires an argument with spaces in

I am attempting to execute a bash script from the command line. Sometimes it will require an argument that contains spaces.

bash test.sh -l "path/to/image/may contain spaces.png"

What is the correct way to provide this? I have tried escaping \ but to no avail.

I receive the following error;

 bash splash.sh -b \#\000000 -l \"img/Rev Logo.jpg\"
    convert: unable to open image ''"img/Rev'': No such file or directory @ error/blob.c/OpenBlob/3093.

Which relates to this bit of code;

logo="$1"

if checkBackground -eq 1
        then 
        convert -size $size canvas:$back -background $back $tmp01
fi

convert \'$logo\' -background $back $tmp02

composite $tmp02 -gravity center $tmp01 output.jpg

Upvotes: 0

Views: 55

Answers (1)

William Pursell
William Pursell

Reputation: 212594

If the first argument to the script is "foo bar", then this line:

convert \'$logo\' -background $back $tmp02

is passing as its first argument to convert the string 'foo and passing the second argument bar'. You probably don't want that. Change that line to:

convert "$logo" -background "$back" "$tmp02"

In general, double quote any variables.

And when you invoke the script, just quote the argument. If you call the script as:

bash splash.sh -b \#\000000 -l \"img/Rev Logo.jpg\"

then you are passing two separate arguments: "img/Rev and Logo.jpg". It is perfectly valid to have a filename with a double quote in it, but very unusual. If you want to pass the single argument img/Rev Logo.jpg, you want to invoke your script as:

bash splash.sh -b "#000000" -l "img/Rev Logo.jpg"

or

bash splash.sh -b \#000000 -l img/Rev\ Logo.jpg

Upvotes: 2

Related Questions