Skip to main content
Solved

Path with Spaces in a Variable


Forum|alt.badge.img+8

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

Best answer by mm2270

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 original
Did this topic help you find an answer to your question?

2 replies

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • Answer
  • December 5, 2018

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


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 143 replies
  • December 6, 2018

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings