Access path space environment variables

Asked

Viewed 427 times

1

I created an environment variable that stores a path roomy:

/c/Users/Bruno Xavier

Despite being in windows, my intention is to use it by git bash for easy access to the directory.

I tried the two most obvious alternatives:

  • Encapsulate the path with quotes: "/c/Users/Bruno Xavier"
  • Skip the white space: /c/Users/Bruno\ Xavier

None of them worked. The only solution I can come up with is:

"$my_var" com my_var = /c/Users/Bruno Xavier

However, I would like to access cd $my_var without having to type the quotes in the terminal

  • What is your doubt after all?

  • I want to type $my_var and not "$my_var"

1 answer

0

I don’t think it’s possible, the way bash works.

If you have in the variable $my_var the directory /c/Users/Bruno Xavier, execution of the following command without quotation marks:

cd $my_var

It gives the shell to understand that you want to make a cd /c/Users/Bruno and Xavier:

cd /c/Users/Bruno Xavier

What for the cd doesn’t make much sense. First because there is no directory /c/Users/Bruno and second because there is no parameter Xavier to be interpreted as argument from the command cd.

For this reason, double quotation marks must be passed:

cd "$my_var"

If you have any impediment to using the pairs in your problem, I suggest opening a new question explaining what is this impediment, to direct to a different answer.

  • That’s a good explanation. But I still don’t understand the case cd $my_var where my_var="/c/Users/Bruno Xavier" with quotation marks on the environment variable.

  • What’s the question? If you just do cd my_var I think bash will understand that he wants to go in a directory called my_var not that it wants to read the contents of the variable $my_var. That would be the question?

  • You missed it. It would be cd $my_var

  • Regardless of whether the variable was saved with double quotes or not, bash will translate the variable to /c/Users/Bruno Xavier. Without double quotes at the time of making the cd, the problem described will happen.

  • If I slip away /c/Users/Bruno\ Xavier? cd should not now be able to find the directory?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.