0

I am trying to add an alias in bash_profile in Mac. Actual path of file I am trying to create alias is

amar@admin:~/Library/Application Support/com.bohemiancoding.sketch3/Plugins 

$pwd

/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins

I have tried 2 options

Option # 1

export sketch="/Users/amar/Library/Application\ Support/com.bohemiancoding.sketch3/Plugins"

This has error like

amar@admin:~$ echo $sketch
/Users/amar/Library/Application\ Support/com.bohemiancoding.sketch3/Plugins
amar@admin:~$ cd $sketch
-bash: cd: /Users/amar/Library/Application\: No such file or directory

Option # 2

export sketch="/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins"

this shows error like

amar@admin:~$ echo $sketch
/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins
amar@admin:~$ cd $sketch
-bash: cd: /Users/amar/Library/Application: No such file or directory

Issue is near this file name Application Support

Amarjit Dhillon
  • 2,718
  • 3
  • 23
  • 62
  • 1
    There is a neat option, `shopt -s cdable_vars`, that allows to keep a directory path in a variable, like `export sketch='/path/with space/'`, and then you can use `cd sketch` from anywhere in the filesystem. See [the manual](https://www.gnu.org/software/bash/manual/bash.html#The-Shopt-Builtin) and [this Q&A](https://stackoverflow.com/questions/17958567/how-to-make-an-alias-for-a-long-path/39839346#39839346). – Benjamin W. Nov 08 '17 at 05:38

1 Answers1

3

You have to quote the variable when you expand it:

sketch="/Users/amar/Library/Application Support/com.bohemiancoding.sketch3/Plugins"
cd "$sketch"

Quoting is not optional and there's nothing you can do to avoid it.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • yeah, it worked, but why I don't have to specify " " in this case. export flink="/usr/local/Cellar/apache-flink/1.3.2" . here cd $flink also works – Amarjit Dhillon Nov 08 '17 at 04:06
  • 1
    That string doesn't contain any metacharacters like space/*/[]/? – that other guy Nov 08 '17 at 04:40
  • 1
    @AmarjitSingh When you use a variable without double-quotes, the shell splits it into "words" (based on spaces, tabs, and linefeeds), and then tries to expand any wildcards (`*`, `?`, `[]`) it finds into lists of matching filenames. It does not pay attention to escapes or quotes in the variable's value when it does this. Occasionally, this is what you want, but (in my experience) usually not. So of the variable contains (or *might* contain) any of these characters, you should double-quote it. – Gordon Davisson Nov 08 '17 at 05:41