I want to create an alias that will use bash commands like pwd. Like
alias myalias="myprogram $(pwd)".
But defined like this the alias will be evaluated when the alias is loaded and not when I run my alias. How can I achieve that ?
Simply escape with \:
alias myalias="myprogram \$(pwd)"
This results in:
$ alias myalias
alias myalias='myprogram $(pwd)'
and $(pwd) gets evaluated when you run myalias.
Related
I have a bad habit of putting spaces in my folder/file names. Today it bites me.
I have a folder called NFB Lab in which I installed NFB Lab. I wanted to add the shortcut/command nfb and pynfb to the ~/.zshrc file to start the main python script from anywhere.
I edited the ~/.zshrc file through nano with:
alias nfb=/Users/mathieu/Documents/NFB\ Lab/pynfb/main.py
alias pynfb=/Users/mathieu/Documents/NFB\ Lab/pynfb/main.py
I also tried:
alias nfb="/Users/mathieu/Documents/NFB Lab/pynfb/main.py"
alias pynfb="/Users/mathieu/Documents/NFB Lab/pynfb/main.py"
Neither works, I always get:
zsh: no such file or directory: /Users/mathieu/Documents/NFB
How can I solve this without uninstall/reintsall of NFB Lab?
You'll need to escape the space (\ ), for example, take a look at my sublimetext3 alias;
alias sub='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
Otherwise, take a look at ZSH functions. There are many more options compared to aliasses;
For example, run python script with arg as path, then create an alias calling that function
function runpy() {
python3 "$#"
}
alias runx="runpy '/tmp/dir with space/py.py'"
alias runy="runpy '/tmp/dir with space/py_second.py'"
You need two backslashes.
$ mkdir "f oo"
$ alias f="cd f\\ oo"
$ f
$ pwd
/home/foobar/tmp/f oo
Considering the confusion caused by backslash inside single or double quotes, here is one alternative :
alias nfb="'/Users/mathieu/Documents/NFB Lab/pynfb/main.py'"
alias pynfb="'/Users/mathieu/Documents/NFB Lab/pynfb/main.py'"
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.)
I'm using Bash via the mintty terminal on Cygwin, and I've created two aliases in my .bashrc file in my Cygwin home directory.
alias croot="C:/cygwin64"
alias desktop="B:/Users/User/Desktop"
When I enter croot or desktop into the terminal, it seems to work fine:
B:/Users/User/Desktop: Is a directory
However, using those aliases with something like cd croot returns the error:
-bash: cd: croot: No such file or directory
What's going on here?
alias doesn’t work the way you think it does. Do this:
alias croot='cd C:/cygwin64'
croot
Or:
croot=C:/cygwin64
cd "$croot"
Result:
$ pwd
/
There is a way to make this work. But I would not recommend it. Use steven's answer instead.
$ help alias
alias: alias [-p] [name[=value] ... ]
Define or display aliases.
Without arguments, 'alias' prints the list of aliases in the reusable
form 'alias NAME=VALUE' on standard output.
Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
Options:
-p print all defined aliases in a reusable format
Exit Status:
alias returns true unless a NAME is supplied for which no alias has been
defined.
$ alias croot="C:/cygwin64"
$ alias desktop="B:/Users/User/Desktop"
$ alias cd='builtin cd ' # Notice the trailing space.
$ cd croot; pwd
/
Note that only the word immediately next to cd will be considered for alias expansion. Hence cd -P croot will not work.
I made my own script to move "up" on unix (cd ..) and it is called like this: $ /path/to/script/myup args | cd where args may be nothing. I would like to alias this to "up" on my tcsh and bash shells so I can call it: $ up args. I thought it would be aliased like: alias up '/path/to/script/myup \!*| cd' because that is what I used for my cdls alias but its not working. How do I properly pass parameters to an alias?
perhaps you mean cd
`/path/to/script/myup\\!*`
This execute myup and passes its output to cd as a parameter. Note that they are backwards quotes
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)")"