95
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 ...
- 27.4k
90
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 -...
- 4,054
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 ...
- 128k
49
votes
How to have tail -f show colored output
Have a look at lnav, the advanced log file viewer.
It can also pretty print various formats.
Before:
After:
- 591
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. ...
- 311k
39
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() {
>...
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)
...
- 504k
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 ...
- 311k
26
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 ...
- 82.5k
25
votes
Have xargs use alias instead of binary
Use alias xargs='xargs '
alias: alias [-p] [name[=value] ... ]
(snip)
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
- 1,161
25
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 ...
- 7,330
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 ...
- 311k
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 ...
- 311k
24
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,...
- 311k
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 ...
- 311k
22
votes
Is it possible to include file in config file of zsh? How?
To source a file if it exists in one line:
[ -f .aliases ] && source .aliases
- 1,152
22
votes
Accepted
How do you make an alias or function that retains tab completion?
First find out which complete-function is used for the systemctl command:
complete | grep " systemctl$"
The output looks like this:
complete -F _functionname systemctl
Then use:
complete -F ...
- 46.9k
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 '
- 2,991
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 ...
- 67.8k
21
votes
How to reset a shell environment?
To undo exported environment variables, you can use the env command env -i zsh. env runs the command given as an argument, and -i gives it a clean environment.
Depending on your configuration, you ...
- 1,491
21
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, ...
21
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 ...
- 128k
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
> ...
- 21.3k
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/...
- 49.4k
19
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 ...
- 311k
19
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'
- 55.5k
19
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 ...
- 40.8k
18
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 ...
- 311
18
votes
Run a command in an interactive shell with ssh after sourcing .bashrc
The problem is that you are trying to run an alias in a non-interactive shell. When you run ssh user@computer command, command is run non-interactively.
Non interactive shells don't read aliases (...
- 228k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
alias × 868bash × 483
shell × 160
zsh × 108
shell-script × 62
linux × 50
command-line × 49
function × 49
quoting × 33
ssh × 29
cd-command × 25
sudo × 22
tcsh × 22
macos × 19
csh × 19
oh-my-zsh × 18
terminal × 17
grep × 16
autocomplete × 16
scripting × 15
vim × 15
environment-variables × 15
command × 15
command-history × 15
directory × 14