Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Process.Start, bin/bash...and quotes

Discussion in 'macOS' started by jmdeb, Jul 7, 2022.

  1. jmdeb

    jmdeb

    Joined:
    Jul 28, 2017
    Posts:
    23
    Hello to everyone,
    I'm going completely crazy with this problem ;)
    I'm trying to launch an application (in this example, an emulator such as MAME or RetroArch) with path parameters...which can obviously include spaces.

    Launched by 'Terminal' this line runs correctly:
    Code (JavaScript):
    1. "/Users/imac/Downloads/MAME_for_MacOSX_0.212-64bit/mame64" -rompath "/Users/imac/Documents/Emulation/MAME/Roms OK" 88games.zip
    Once in Unity, this code also works:
    Code (CSharp):
    1. ProcessStartInfo startInfo = new ProcessStartInfo()
    2. {
    3.     FileName = "/bin/bash",
    4.     UseShellExecute = false,
    5.     Arguments = "-c \"" + "/Users/imac/Downloads/MAME_for_MacOSX_0.212-64bit/mame64" + " -rompath " + "/Users/imac/Documents/Emulation/MAME/Roms" + " " + "88games.zip" + "\""
    6. };
    7. Process myProcess = new Process
    8. {
    9.     StartInfo = startInfo
    10. };
    11. myProcess.Start();
    12. myProcess.WaitForExit();
    ...but that's because there is not a single space in any of the paths or filenames! As soon as there is a space, nothing to do it doesn't work! I can add quotes and escape them or double or triple quotes ;)...nothing works!

    Let's take for instance the last path:
    "/Users/imac/Documents/Emulation/MAME/Roms"
    ...and imagine there is a space in it like so:
    "/Users/imac/Documents/Emulation/MAME/Roms OK"
    I surround it by escaped quotes:
    "\"/Users/imac/Documents/Emulation/MAME/Roms OK\""
    ...but it will never succeed!
    What is please my error? It's like obviously the quoted path is never 'seen' by bin/bash!

    Thanks a lot for any help!
     
    Last edited: Jul 7, 2022
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    I had a similar issue recently with bash scripts. When you put a space in the arguments list it treats it as a new parameter, regardless of whether it was quoted or not. I got round the problem by escaping the space itself.

    For example,

    \"Roms OK\" would become:

    \"Roms\ OK\"
     
  3. jmdeb

    jmdeb

    Joined:
    Jul 28, 2017
    Posts:
    23
    Thanks for you reply tone!
    I finally got it working by the joint use of single quotes, substitution variables and escaped double quotes...thanks to those 2 posts https://unix.stackexchange.com/a/144519, https://stackoverflow.com/a/26167919

    So, here is the theory:
    Code (CSharp):
    1. /bin/bash -c 'echo "$0" "$1"' foo bar
    …and put into practice
    Code (CSharp):
    1. ProcessStartInfo startInfo = new ProcessStartInfo()
    2. {
    3.     FileName = "/bin/bash",
    4.     UseShellExecute = false,
    5.     CreateNoWindow = false,
    6.     Arguments = "-c '\"/Users/imac/PathToThe/Application.app/Contents/MacOS/Application\" \"$0\" \"$1\" \"$2\"' \"argument_or_Path_1\" \"argument_or_Path_2\" \"argument_or_Path_3\""
    7. };
    8. Process myProcess = new Process
    9. {
    10.     StartInfo = startInfo
    11. };
    12. myProcess.Start();
    13. myProcess.WaitForExit();
     
    Last edited: Jul 9, 2022
    tonemcbride likes this.
  4. sameng

    sameng

    Joined:
    Oct 1, 2014
    Posts:
    172