0

I have Windows operating system. I am using GIT BASH. I have written script run.sh and run it in this way:

$ ./run.sh

This code works correctly:

dir="/c/Windows"
app="notepad.exe"
$dir/$app

application notepad will open as a result.


But I can't run the following code:

dir="/c/Program Files/7-Zip"
app="7zFM.exe"
$dir/$app

as a result a get an error message:

./run.sh: line 63: /c/Program: No such file or directory

I try this:

dir='/c/"Program Files"/7-Zip'
app="7zFM.exe"
$dir/$app

as a result:

./run.sh: line 63: /c/"Program: No such file or directory

Then i try this:

dir=/c/Program\ Files/7-Zip
app="7zFM.exe"
$dir/$app

also error:

./run.sh: line 63: /c/Program: No such file or directory

How can i use space in directory name in GIT BASH ?

alex_t
  • 71
  • 1
  • 6

1 Answers1

0

Unrelated question, but the answer is to the point - c.f. Replace String in variable using bash

Short answer - always quote your variables unless you know WHY you shouldn't in a specific circumstance.

$dir/$app       # doesn't work: /c/Program: No such file or directory
"$dir/$app"     # works fine
"$dir"/"$app"   # works fine

You can also combine them.

p="$dir/$app"   # assignment works
$p              # doesn't work: /c/Program: No such file or directory
"$p"            # works fine.

You have to protect the embedded space when it's being used.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36