DSpencer
DSpencer

Reputation: 31

How to write a bash script alias with whitespace on macOS

UPDATE:

I made progress, but I'm still unsure what I'm not understanding. I've reduced my script down to a single line, and I still run into the same issue.

Any insight on the following?
NOTE: if I enter "/Users/Da.../Docker" or as it is below using a backspace for the whitespace, the result is the same.

for myvm in $(find /Users/David/Documents/Virtual\ Machines/Docker -name *.vmx); do echo -e "VM name: $myvm"; done VM name: /Users/David/Documents/Virtual VM name: Machines/Docker/RHEL7-DockerHost-Node04.vmwarevm/RHEL7-DockerHost-Node04.vmx ... VM name: /Users/David/Documents/Virtual VM name: Machines/Docker/RHEL7-DockerHost.vmwarevm/RHEL7-DockerHost.vmx VM name: /Users/David/Documents/Virtual VM name: Machines/Docker/RHEL7-DockerHost-Node01.vmwarevm/RHEL7-DockerHost-Node01.vmx

What am I missing?

--------------------------

I've found similar questions, but the answers aren't working for me.

I'm writing a script that will allow me to start and stop multiple VMs within VMware Fusion without having to click each one using the vmrun command, found at "/Applications/VMware Fusion.app/Contents/Library/vmrun".

Problem

When I try to alias vmrun:

alias myvmcli='/Applications/VMware\ Fusion.app/Contents/Library/vmrun'

...I get the following error from bash:

line 3: /Applications/VMware\: No such file or directory

Discussion

It obviously is missing the whitespace, but I thought using the backspace character would indicate that whitespace should be used.

I've tried single quotes and double quotes in the alias line to no avail.

If I run alias from the command line, it works fine.

Solution/Thoughts?

I'm sure that creating a link can solve my problem, but I really want to know why this isn't working.

Upvotes: 3

Views: 2415

Answers (3)

chepner
chepner

Reputation: 531718

You can avoid both functions and aliases by adding the directory to your path:

PATH="/Applications/VMware Fusion.app/Contents/Library:$PATH"

Then vmrun by itself will work.

Upvotes: 2

cdarke
cdarke

Reputation: 44394

A backslash \ is not required inside quotes, it is retained. You would only need the backslash if you didn't use quotes, but generally quotes are good.

$ echo 'Hello\ world'
Hello\ world
$ echo "Hello\ world"
Hello\ world
$ echo Hello\ world
Hello world
$ echo "Hello world"
Hello world

Using an alias inside a script is not good, aliases are really designed as productivity aids on the command-line.

In this case just assign it to a variable, you don't need an alias or a function.

cmd='/Applications/VMware Fusion.app/Contents/Library/vmrun'

Then, when you want to run it:

"$cmd"

Note the double quotes, required because of the embedded space.

Tested on OS X with vmware.

As others have said, something more complex than this would require a function.

Upvotes: 1

Sakis
Sakis

Reputation: 91

You need to quote twice:

alias myvmcli='"/Applications/VMware Fusion.app/Contents/Library/vmrun"'

or escape:

alias myvmcli=\''/Applications/VMware Fusion.app/Contents/Library/vmrun'\'

or

alias myvmcli="\"/Applications/VMware Fusion.app/Contents/Library/vmrun\""

Be careful of the way the 2nd and 3rd are escaped. They differ.


You need the double quotation because you need to quote once because the alias command requires it (alias asd="echo asd"), and then once again because the command inside the alias requires it.


EDIT:

Even though I answered this, the alias you posted, works with me just fine. Could be due to differences in bash version though:

[  0][s: ~ ]$ cd /tmp
[  0][s: tmp ]$ alias asd='asd\ zxc/test'
[  0][s: tmp ]$ asd
woooo!

Upvotes: 1

Related Questions