Path with Spaces in a Variable

strider_knh
Contributor II

I am working on a script that will check if an application is present and then run some commands. I am trying to write this so that I can easily change it later for a different application so I am putting the application and the path in a variable. The problem I have run into is that there are spaces in the path and so I am having trouble getting the IF statement to work and other commands. I think it is just where and when to put quotes and the character. If I take out the variable and place the path directly everything is fine.

Below is the main part I am trying to get working. If I can get that to work then I am sure I can get the rest of it.

#!/bin/bash Apple_Installer="/Applications/Install macOS Mojave.app" if [ -d "$Apple_Installer" ]; then Installer_Version=$(/usr/libexec/PlistBuddy -c 'Print CFBundleShortVersionString' "$Apple_Installer"/Contents/Info.plist) else echo "Not Present" fi exit 0
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

You don't need any 's in your path if you are double quoting the path. Quoting handles the spaces without needing to escape them that way.
So change this line

Apple_Installer="/Applications/Install macOS Mojave.app"

to

Apple_Installer="/Applications/Install macOS Mojave.app"

and it should work fine.

*Edited to remove the second backslash

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

You don't need any 's in your path if you are double quoting the path. Quoting handles the spaces without needing to escape them that way.
So change this line

Apple_Installer="/Applications/Install macOS Mojave.app"

to

Apple_Installer="/Applications/Install macOS Mojave.app"

and it should work fine.

*Edited to remove the second backslash

strider_knh
Contributor II

Ya, yes. That worked, thank you.

I had tried that previously but was still having an issue with a different command but later discovered that I had missed the quotes in that command.

Thank you.