113
votes
How to pass parameters to an alias?
Alias solution
If you're really against using a function per se, you can use:
$ alias wrap_args='f(){ echo before "$@" after; unset -f f; }; f'
$ wrap_args x y z
before x y z after
You can replace ...
105
votes
How do I get bash completion for command aliases?
Try complete-alias, which solves this problem exactly. (Disclaimer: I am the author of complete_alias)
After install it you can use one generic function to complete many aliases like this:
complete -...
52
votes
Accepted
Would globally aliasing the fork bomb prevent its execution?
The two, no, three, ... Amongst the main obstacles to that are:
It's not a valid name for an alias. Bash's online manual:
The characters ... and any of the shell metacharacters or quoting ...
43
votes
Accepted
Alias and functions
Aliases are expanded when a function definition is read,
not when the function is executed …
$ echo "The quick brown fox jumps over the lazy dog." > myfile
$ alias myalias=cat
$ myfunc() {
>...
42
votes
Alias to CD in a directory and call a command
To execute a command with a specific working directory, one usually does
( cd directory && utility )
The parentheses around the cd ... means that the command(s) therein runs in a subshell. ...
39
votes
Accepted
Are there any single character bash aliases to be avoided?
Things to avoid:
standard or common commands with single character names: w (show logged in users' activity), X (X Window System server), R (R programming language interpreter), [ (similar to test)
...
35
votes
Accepted
How to make a multiline alias in Bash?
It's not impossible at all.
alias thing='(
cd "${program_to_update_dir}"
wget "https://raw.githubusercontent.com/USER/PROJECT/BRANCH/update.sh"
source update.sh
rm update.sh
)'
or,...
34
votes
Only if in "/" : alias ls='ls -I test'?
ls () {
case "$PWD" in
/) command ls -I test "$@" ;;
*) command ls "$@" ;;
esac
}
The above shell function will test the current directory against / and executes the GNU ls command ...
32
votes
Fish-Shell Will Not Save my Aliases
A fish alias is actually implemented as a function. To save a function, you need funcsave. So this is the sequence
alias foo=bar
funcsave foo
That creates ~/.config/fish/functions/foo.fish which will ...
30
votes
How can I test if a particular alias is defined?
If alias is passed an alias name without =value, it just prints the alias definition if that alias is defined, or fails with an error if there's no such alias.
So you can just do:
if alias ...
26
votes
Accepted
How to create an environment variable that is the output of a command
In general the sequence foo="$(bar)" will run the command bar and assign the output to the variable.
e.g.
% echo $PWD
/home/sweh
% BWD="$(basename "$PWD")"
% echo $BWD
sweh
This creates a shell ...
26
votes
Accepted
How can I alias `...` to `../..` in Bash?
If switching to zsh is an option, you could use global aliases there for that:
alias -g ...=../..
But in any case that's only expanded when ... is recognised as a full delimited token on its own. It ...
24
votes
Accepted
How to stop .bashrc from running sub-command in alias at startup?
Use pkill instead:
alias killguake='pkill guake'
This is a whole lot safer than trying to parse the process table outputted by ps.
Having said that, I will now explain why your alias doesn't do ...
24
votes
Making alias of rm command
An alias can not take arguments and use $@ to access them like that.
Alias expansion in bash is a simple text replacement. If you have alias rm ='something something', then using rm file1 file2 would ...
23
votes
Accepted
-bash: /bin/cd: No such file or directory - automatically execute ls after cd
Your system (like many Unix systems) does not have an external cd command (at least not at that path). Even if it had one, the ls would give you the directory listing of the original directory. An ...
22
votes
Accepted
Trying to use `pwd` inside an alias giving unexpected results
Use single quotes to avoid shell expansion at time of definition
alias search='find `pwd` -name '
22
votes
Only if in "/" : alias ls='ls -I test'?
Use a function that tests if you're in / for ls:
ls () {
if [[ "$PWD" == / ]]
then
command ls -I test "$@"
else
command ls "$@"
fi
}
This way, any arguments you pass ...
22
votes
Accepted
How to show what an alias means
You can use alias (without equals) or type
$ alias l
alias l='ls -CF'
$ type l
l is aliased to `ls -CF'
22
votes
Accepted
nohup: failed to run command `.': Permission denied
nohup runs an executable. You need to pass it an external command, i.e. an executable file. You can't call nohup on a shell construct such as an alias, function or builtin. nohup runs a new process, ...
22
votes
Accepted
bash aliases do not expand even with shopt expand_aliases
It doesn't seem work if you set the alias on the same line as it's used. Probably something to do with how aliases are expanded really early in the command line processing, before the actual parsing ...
22
votes
Accepted
How to make `python` an alias of `python3` systemwide on Debian
It looks like Debian is now shipping python-is-python3 themselves (in Debian 11 and later), so the premise of the question no longer holds and you can just: sudo apt update && sudo apt install ...
21
votes
Accepted
Can't use alias in script, even if I define it just above!
Simply don't use aliases in scripts. It makes little sense to use a feature designed for interactive use in scripts. Instead, use functions:
somecommand () {
ls -alF
}
Functions are much more ...
20
votes
How do I get bash completion for command aliases?
By googling this issue I ended up here, so I tried the approaches in the other answers. For various reasons I don't actually understand, they never behaved properly in my Ubuntu 16.04.
What in the end ...
20
votes
How to copy or move files without being asked to overwrite?
For force overwrite without asking you should use the command mv and the option "-f", use man to see the options.
man mv:
-f, --force
do not prompt before overwriting
Example:
mv -f ...
20
votes
Accepted
How to save an alias of an eval $(other_comand) command
Enclose your alias in single quotes instead of double quotes.
alias dockereval='eval $(docker-machine env)'
Double quotes allow expansion of variable (in bash at least) while single quotes don't
20
votes
Accepted
Multiples aliases for one command
You could use brace expansion.
alias {name1,name2}="echo hello"
or for your example
alias lw{p,b}c="$(npm bin)/webpack"
https://www.gnu.org/software/bash/manual/html_node/Brace-...
20
votes
Accepted
why doesn't gdb like aliases
Aliases are a feature of the shell. Defining an alias creates a new shell command name. It's recognized only by the shell, and only when it appears as a command name.
For example, if you type
> ...
20
votes
Are there any single character bash aliases to be avoided?
The simplest way is probably to check whether something with that name already exists. On my system:
$ for char in {A..z}; do type "$char" 2>/dev/null; done
R is /usr/bin/R
X is /usr/bin/...
20
votes
Accepted
Multi-word aliases in bash
Well, I get:
$ alias 'apt update'='sudo apt update -y'
bash: alias: 'apt update': invalid alias name
which seems clear enough.
Similarly, you can't have functions or commands with whitespace in their ...
19
votes
How to create alias for ssh command?
I usually use the .ssh/config file found in my home directory to run my OpenSSH client in the format ssh ubserver. In that file found here /home/$USER/.ssh I have the follow configurations:
Host ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
alias × 901bash × 503
shell × 162
zsh × 115
shell-script × 65
linux × 52
command-line × 51
function × 51
quoting × 34
ssh × 30
cd-command × 25
tcsh × 23
sudo × 22
csh × 20
scripting × 19
macos × 19
oh-my-zsh × 18
terminal × 17
grep × 16
environment-variables × 16
autocomplete × 16
vim × 15
command × 15
command-history × 15
directory × 14