alias wont work - embedding a quoted string in an alias definition - bash

Im trying to make an alias on my mac terminal in zsh shell.
some info when i do echo $SHELL i get /bin/zsh proving im using the zsh shell.
mac has a command you can use to open things in chrome like this :
open -a "google chrome" myfile.html
I need to make an alias of this but it wont work but other aliases work.
i would like the alias to run like this
chrome myfile.html
here is my alias file:
alias x="echo \'y "
alias chrome="open -a \'google chrome\' "
so if i run x at the prompt y does get outputed but if i run chrome at the command line it says it cant find the file. Do i have to use xargs or something ?

Note: This answer applies not only to zsh, but to bash and ksh as well.
Try:
alias chrome='open -a "Google Chrome"'
By using single quotes around the entire definition (which is generally preferable when defining an alias, so as to prevent premature expansion of the string at alias definition time rather than at execution time), you can use double quotes inside without escaping.
The problem with your definition was that \' literally became part of the alias (rather than just ') - there is no need to escape ' in a double-quoted string literal.
Thus, you can also use the following (but, as stated, using single quotes to enclose the entire definition is preferable):
alias chrome="open -a 'Google Chrome'"

You don't need to quote the single quotes within the double quotes, otherwise you'll end up with literal quotes. Just try it with echo:
$ alias chrome="echo open -a \'google chrome\' "
$ chrome
open -a 'google chrome'
The quotes that are shown here are literal parts of the argument.
Just use one of:
alias chrome="open -a 'google chrome'"
alias chrome='open -a "google chrome"'
alias chrome="open -a \"google chrome\""
All of them are equivalent.

Related

.bashrc command to copy current working directory

I am trying to write an alias for the following command that takes your current working directory, surrounded by quotes, and copies it to the clipboard.
echo \"$(pwd)\" | xclip
However, writing an alias for this in .bashrc is not working quite right. This is the alias I have written:
alias cpwd="echo \"\$(pwd)\" | xclip"
But when that command is ran the double quotes are omitted. I have checked answers to similar problems, such as bash alias command with both single and double quotes, but I think I am escaping all the required characters, so I do not know why this command isn't working.
The current result of the command would be something like: home/user/My Folder rather than "home/user/My Folder" like I am wanting.
You need more escaping. You've correctly escaped the $; you need to do the same with the existing backslashes.
alias cpwd="echo \\"\$(pwd)\\" | xclip"
Alternatively, you can avoid all the escaping by using single quotes.
alias cpwd='echo \"$(pwd)\" | xclip'
Best of all, use a function instead of an alias. A function lets you write the command exactly as you would normally without any extra quotes or escaping.
cpwd() {
echo \"$(pwd)\" | xclip
}

Use semicolon character ";" in bash alias

Got a new annoying keyboard and I'm always typing ;s instead of ls.
Is there a way to create a sort of alias for this?
I tried this: alias ;s="ls" but of course it does not work!
You cannot. You can escape the ; when you run the command, but then alias itself informs you that the name is invalid.
$ alias \;s=ls
bash: alias: `;s': invalid alias name
You could define a Readline macro to replace ;s with ls before the shell tries to parse it:
$ bind '";s": "ls"'
This command can be added to your .bashrc file. (You could add it a definition to .inputrc, but it is unlikely you would want to use this macro in any other Readline-aware program.)

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.
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.
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!
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.

Shell Alias Which Whitespace Cancelling

When creating an alias for a binary tool in a folder that contains a space, the alias is correctly stored, but when the command is called, the space is evaluated as if it isn't properly cancelled.
The binary is viewable in $PATH. I ran this from zsh 4.3.11 and bash 3.2.48, both with the same result.
Binary Path
~/Test Folder/fooBinary
Alias
alias foo="`which fooBinary`"
This results in
foo='~/Test Folder/fooBinary'
Now calling this alias results in
[shell]: no such file or directory: ~/Test
This used to work to escape the spacing from the alias and I didn't bother to check the version of my older shell or I would go find it.
The I did to actually escape the spacing:
alias foo="'`which fooBinary`'"
My questions:
Why are the spaces evaluated in the quoted alias?
Is there a better way of escaping which?
Aliases have always split on spaces. This is intentional and useful as it allows you to alias arguments, e.g. alias rm='rm -i' or alias commit='git commit -a'.
If you need to quote an argument programmatically, you can use printf %q to add a level of escaping:
alias foo="$(printf %q "$(which fooBinary)")"

Combining variables in Bash to form a command sent to AppleScript using the osascript command

I'm trying to make this script work. It's a Bash script that is meant to take some variables, put them together and use the result to send an AppleScript command. Manually pasting the string echoed from the variable to_osa behind osascript -e to the terminal works as I want and expect it to. But when I try to combine the command osascript -e and the string to_osa, it does not work. How can I make this work?
the_url="\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\""
the_script='tell application "Safari" to set the URL of the front document to '
delimiter="'"
to_osa=${delimiter}${the_script}${the_url}${delimiter}
echo ${to_osa}
osascript -e ${to_osa}
In addition to working manually the script also works when I write the desired command to a script and then execute it:
echo "osascript -e" $to_osa > ~/Desktop/outputfile.sh
sh ~/Desktop/outputfile.sh
String mashing executable code is error prone and evil, and there's absolutely no need for it here. It's trivial to pass arguments to an AppleScript by defining an explicit 'run' handler:
on run argv -- argv is a list of strings
-- do stuff here
end run
which you then invoke like so:
osascript -e /path/to/script arg1 arg2 ...
BTW, if your script requires a fixed number of args, you also write it like this:
on run {arg1, arg2, ...} -- each arg is a string
-- do stuff here
end run
...
Going further, you can even make the AppleScript directly executable as you would any other shell script. First, add a hashbang as follows:
#!/usr/bin/osascript
on run argv
-- do stuff here
end run
then save it in uncompiled plain text format and run chmod +x /path/to/myscript to make the file executable. You can then execute it from the shell as follows:
/path/to/myscript arg1 arg2 ...
Or, if you don't want to specify the full path every time, put the file in /usr/local/bin or some other directory that's on your shell's PATH:
myscript arg1 arg2 ...
...
So here's how you should be writing your original script:
#!/bin/sh
the_url="http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash"
osascript -e 'on run {theURL}' -e 'tell application "Safari" to set URL of document 1 to theURL' -e 'end run' $the_url
Quick, simple, and very robust.
--
p.s. If you'd rather open a URL in a new window rather than an existing one, see the manpage for OS X's open tool.
As a general rule, don't put double-quotes in the variable, put them around the variable. In this case it's more complicated, since you have some double-quotes for bash-level quoting, and some for AppleScript-level quoting; in this case, the AppleScript-level quotes go in the variable, the bash-level quotes go around the variable:
the_url="\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\""
the_script='tell application "Safari" to set the URL of the front document to '
osascript -e "${the_script}${the_url}"
BTW, using echo to check things like this is highly misleading. echo is telling you what's in the variable, not what'll be executed when you reference the variable on a command line. The biggest difference is that echo prints its arguments after they've been through bash parsing (quote and escape removal, etc), but when you say "Manually pasting the string ... works" you're saying it's what you want before parsing. If the quotes are there in the echoed string, that means bash didn't recognize them as quotes and remove them. Compare:
string='"quoted string"'
echo $string # prints the string with double-quotes around it because bash doesnt't recognize them in a variable
echo "quoted string" # prints *without* quotes because bash recognizes and removes them

Resources