Use semicolon character ";" in bash alias - bash

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

Related

Difference between a semicolon and a new line in bash ("source rcfile; foo" vs. "source rcfile<NEWLINE>foo" where "foo" is an alias defined in rcfile)

I thought that giving a semicolon in bash is the same as giving a new line, just combining multiple lines.
However, with alias sudo='sudo -i' in the .bashrc file, the below result doesn't seem to work as I expected:
USER#HOST:~:$ cat tmp-1.sh
#!/bin/bash
source ~/.bashrc; sudo env | grep PATH
USER#HOST:~:$ cat tmp-2.sh
#!/bin/bash
source ~/.bashrc
sudo env | grep PATH
USER#HOST:~:$ ./tmp-1.sh
PATH=/usr/bin:/bin:/usr/sbin:/sbin
USER#HOST:~:$ ./tmp-2.sh
PATH=/home1/client/bin:/usr/python-3.8.2-r2/bin:/usr/jdk64/jdk1.8.0_112/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin:/root/bin
What's the difference between these two?
As in your comment:
alias sudo='sudo -i' is in .bashrc file
According to man bash:
The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at
least one complete line of input, and all lines that make up a compound command, before executing any
of the commands on that line or the compound command. Aliases are expanded when a command is read,
not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias
definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function
is executed, because a function definition is itself a command. As a consequence, aliases defined in
a function are not available until after that function is executed. To be safe, always put alias
definitions on a separate line, and do not use alias in compound commands.
For almost every purpose, aliases are superseded by shell functions.

~/.zshrc alias with a space

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'"

Why can't I 'cd' into Bash aliases on Cygwin?

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.

How can I escape bash commands in a bash alias

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.

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)")"

Resources