Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a bash script alias with whitespace on macOS

Tags:

bash

shell

sh

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.

like image 676
DSpencer Avatar asked Jan 22 '26 04:01

DSpencer


2 Answers

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.

like image 62
chepner Avatar answered Jan 23 '26 18:01

chepner


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!
like image 45
Sakis Avatar answered Jan 23 '26 16:01

Sakis