Matthew Wolff
Matthew Wolff

Reputation: 90

bash - supplying variable as a file path

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

Upvotes: 1

Views: 2661

Answers (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...

Upvotes: -2

janos
janos

Reputation: 124824

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"

Upvotes: 4

Related Questions