Bash Command Line With Spaces

Ubuntu Unity 24.04

Hi.

My script to start libreoffice:

#!/bin/sh
Rev="25.8"
libreoffice$Rev --$1 $2

My Command line: ./LibreStart writer “Shopping List.odt”

Libreoffice starts up and says:
Shopping does not exist’ and when I acknowledge this, it says:
List.odt’ does not exist.

The Space between ‘Shopping’ and ‘List’ is causing a problem.

How to get libreoffice to treat the Entire 2nd Parameter as a path/filename ?

Thanks,
M…

Put double quotes around “$2” like so:
libreoffice$Rev --$1 "$2"
The shell does word splitting on the values substituted for the parameters. By enclosing the parameter in quotes you’re telling the shell not to split it. Do not use single quotes. Single quotes not only stop word splitting, they also inhibit parameter substitution. If you use single quotes, a literal ‘$2’ would be given to libreoffice as the file name.

4 Likes

To add to the previous comment, you should get into the habit of always double-quoting variables unless you are certain that there aren’t, and never will be, spaces or other special characters.

So, your command line should look like this:

libreoffice"$Rev" --"$1" "$2"

In your case, you are certain that $Rev doesn’t contain a space, and probably never will. I’m guessing that you are also certain that $1 doesn’t contain a space. Therefore, you could use:

libreoffice$Rev --$1 "$2"

But the first one is safest.

1 Like

Thank you guys. Have a nice day,
M…

1 Like