1

I'm trying to specify a variable for opening up a file with a particular app, but no matter how I attempt to reference it, it's not working.

sublime1=/Applications/Sublime\ Text.app/
sublime2="/Applications/Sublime\ Text.app/"
sublime3="/Applications/Sublime Text.app/"

I've been trying different ways of setting the variable, but for each of the variations I've tried, it fails.

open ~/.zshrc -a $sublime1
open ~/.zshrc -a $sublime2
open ~/.zshrc -a $sublime3

The file /Users/matthew/Text.app does not exist

It gives me the same error for each, so I assume they're equivalent. Even when I try cd $sublime it also fails, but slightly differently...

bash: cd: /Applications/Sublime: No such file or directory

Update:
It was suggested by Charles to use a function to accomplish the task of quickly opening something in sublime.

sublime() { open "$@" -a "/Applications/Sublime Text.app/"; }

Will allow you to simply run

sublime ~/.zshrc
Matthew Wolff
  • 90
  • 1
  • 10
  • 3
    Quote `"$sublime1"`, or the shell splits words on spaces. – Benjamin W. Mar 16 '18 at 18:22
  • Possible duplicate of [How to cd into a directory with space in the name?](https://stackoverflow.com/questions/18323508/how-to-cd-into-a-directory-with-space-in-the-name) – Benjamin W. Mar 16 '18 at 18:23
  • 1
    BTW, it's good form to put optional arguments *before* positional ones -- this is the only syntax that [POSIX utility syntax guidelines](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html) guarantee will be supported (search for "Guideline 9"). Thus, `open -a "$sublime1" ~/.zshrc` – Charles Duffy Mar 16 '18 at 18:36
  • Thank you @benjamin, I'm always happy for little tips like this – Matthew Wolff Mar 16 '18 at 18:39
  • 1
    See https://www.sublimetext.com/docs/3/osx_command_line.html. – chepner Mar 16 '18 at 19:03

2 Answers2

4

These assignments are correct:

sublime1=/Applications/Sublime\ Text.app/
sublime3="/Applications/Sublime Text.app/"

The problem is with the invocation. Variables used as command line arguments are subject to word splitting and globbing. You need to double-quote them, like this:

open ~/.zshrc -a "$sublime1"
open ~/.zshrc -a "$sublime3"
janos
  • 120,954
  • 29
  • 226
  • 236
  • This fixes it. I was hoping to save a few keystrokes by defining a `$sublime` variable, but having to put a dollar sign in front and quotes around it almost defeats the purpose. Any way around this? – Matthew Wolff Mar 16 '18 at 18:27
  • 1
    @MatthewWolff, use a function, not a variable. `sublime() { open "$@" -a "/Applications/Sublime Text.app/"; }`, then you can run `sublime ~/.zshrc` – Charles Duffy Mar 16 '18 at 18:27
  • 1
    @MatthewWolff, ...btw, insofar as your goal is to use variables to store commands or parts thereof, see [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050). – Charles Duffy Mar 16 '18 at 18:28
-2

try using sublime1=$(/Applications/Sublime/Text.app)

and using chmod 770 Text.app on Text.app in the command line

sorry for my english...

Matthew Wolff
  • 90
  • 1
  • 10
  • 1
    `var=$(...)` assigns the *output* of `...` to the variable. The OP doesn't want to assign the output of running `Text.app`; instead, they want to store the path to the command itself. – Charles Duffy Mar 16 '18 at 18:27