Tag Info

Hot answers tagged

96

The other answers provide some soft general guidelines based on personal taste, but ignore many pertinent facts that one should consider when deciding between scripts, functions, or aliases. Aliases and Functions ┬╣ The entire contents of aliases and functions are stored in the shell's memory. A natural consequence of this is aliases and functions can ...


90

An alias should effectively not (in general) do more than change the default options of a command. It is nothing more than simple text replacement on the command name. It can't do anything with arguments but pass them to the command it actually runs. So if you simply need to add an argument at the front of a single command, an alias will work. Common ...


71

Try out multitail. This is an ├╝bergeneralization of tail -f. You can watch multiple files in separate windows, highlight lines based on their content, and more. multitail -c /path/to/log The colors are configurable. If the default color scheme doesn't work for you, write your own in the config file. For example, call multitail -cS amir_log /path/to/log ...


40

That's what the command command is for. Try command ls This tells the shell to bypass aliases and functions. This way is supported by bash, zsh, and ash/dash.


38

which is actually a bad way to do things like this, as it makes guesses about your environment based on $SHELL and the startup files (it thinks) that shell uses; not only does it sometimes guess wrong, but you can't generally tell it to behave differently. (which on my Ubuntu 10.10 doesn't understand --skip-alias as mentioned by @SiegeX, for example.) type ...


35

A good way to inspect what a command is: type l If it's a program or a script, it will give you its location, if it is an alias, it will tell you what it's aliased to, if it's a function, it will print the funciton; otherwise, it will tell you if it is a built-in or a keyword. Examples: $ type find find is /usr/bin/find $ type connecthome connecthome is ...


30

First of all, as ddeimeke said, aliases by default are not expanded in non-interactive shells. Second, .bashrc is not read by non-interactive shells unless you set the BASH_ENV environment variable. But most importantly: don't do that! Please? One day you will move that script somewhere where the necessary aliases are not set and it will break again. ...


30

If it's a small number of directories, you can use pushd to rotate between them: # starting point $ pwd /Project/Warnest/docs # add second dir and change to it $ pushd ~/Dropbox/Projects/ds/test ~/Dropbox/Projects/ds/test /Project/Warnest/docs # prove we're in the right place $ pwd ~/Dropbox/Projects/ds/test # swap directories $ pushd /Project/Warnest/docs ...


29

There is a shell variable CDPATH in bash and ksh and cdpath in zsh: CDPATH The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. So you can set in your ~/.bashrc: export CDPATH=/Project/Warnest:~/Dropbox/Projects/ds ...


24

The main difference between aliases and functions is that aliases don't take arguments┬╣, but functions do. When you write something like alias l='ls --color', l foo is expanded to ls --color foo; you can't grab foo into the alias expansion and do something different with it the way you can do with a function. See also How to pass parameter to alias?. ...


23

Personally, I have these in my bashrc and use them all the time: pushd() { if [ $# -eq 0 ]; then DIR="${HOME}" else DIR="$1" fi builtin pushd "${DIR}" > /dev/null echo -n "DIRSTACK: " dirs } pushd_builtin() { builtin pushd > /dev/null echo -n "DIRSTACK: " dirs } popd() { builtin popd > /dev/null echo -n "DIRSTACK: " ...


23

Not a direct answer to your question (since aliases can only be one word), but you should be using git-config instead: git config --global alias.civ commit -v This creates a git alias so that git civ runs git commit -v. Unfortunately, AFAIK there is no way to override existing git commands with aliases. However, you can always pick a suitable alias name ...


21

There's no rc file for grep, but you can set the environment variable GREP_OPTIONS to a whitespace-separated list of options that will apply to all grep commands. This requires GNU grep (the implementation on non-embedded Linux, Cygwin and some other systems) 2.4; the --exclude option is new to GNU grep 2.5. Put something like this in your ~/.profile: ...


21

Something else you might try is a tool called autojump. It keeps a database of calls to it's alias (j by default) and attempts to make intelligent decisions about where you want to go. For example if you frequently type: j ~/Pictures You can use the following to get there in a pinch: j Pic It's installed system-wide, but included on a per-user basis ...


19

The alias is just a defined shortcut. In this example, the alias defined is the string ls, which executes ls --color=auto. If you don't want that behavior, you can call the binary ls using the absolute path. So executing just /bin/ls will produce output without color because it is not the alias you defined. You could also change the alias to something ...


18

If you look into the bash manpage you find: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below). So put a shopt -s expand_aliases in your script. Make sure to source your aliases file after setting this ...


16

Aliases are like commands in that all arguments to them are passed as arguments to the program they alias. For instance, if you were to alias ls to ls -la, then typing ls foo bar would really execute ls -la foo bar on the command line. If you want to have actual control over how the arguments are interpreted, then you could write a function like so: ...


16

Have you had a look at ccze? You have the possibility to customize the default colors of some keywords using the option -c or directly in your configuration file. Edit: If you really would like to have the complete line colored in red, you could also have a try at the following: $ tail -f myfile.log | perl -pe 's/.*SEVERE.*/\e[1;31m$&\e[0m/g' ...


16

I think it's up to each person's taste. For me the logic goes like this: First I try to make an alias, because it's the simplest. If the thing is too complicated to fit in one line, I try to make it a function. When the function starts to grow beyond a dozen of lines I put it in a script. There is really nothing to restrict you from doing something that ...


12

Aliases can't be exported so they're not available in shell scripts in which they aren't defined. In other words, if you define them in ~/.bashrc they're not available to your_script.sh (unless you source ~/.bashrc in the script, which I wouldn't recommend but there are ways to do this properly). However, functions can be exported and would be available to ...


11

Aliases are only expanded as the first argument, or after another alias with a trailing space on the end of the command. From bash's help alias: A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded. To do this, try the following: alias watch='watch ' alias ll='ls -l --color=tty' watch ll ...


11

You're talking about a command that includes a space, but here the command is git and there's no space in there. To call a git commit command, you'd need to write it git\ commit ... 'git commit' ... "git commit" ... Generally commands don't have space in their names for that reason that it is cumbersome to call them in a shell, so I don't think you'll ...


11

Aliases ls is a command, l and la are most likely aliases which make use of the command ls. If you run the command alias you can find all the aliases on your system. $ alias | grep -E ' l=| la=' This will return all the aliases that match the pattern l=... or la=.... Debugging it further You can also use the command type to see how a particular command ...


10

Sourcing the changed file will provide access to the newly written alias or function in the current terminal, for example: source ~/.bashrc An alternative syntax: . ~/.bashrc Note that if you have many instances of bash running in your terminal (you mentionned multiple tabs), you will have to run this in every instance.


10

I would look in /etc/profile.d/ for the offending alias. You could also do the following to find it: grep -r '^alias COMMAND' /etc This will recursively grep through files looking for a line beginning with alias COMMAND. If all else fails, put this at the end of your ~/.bashrc unalias COMMAND


10

You should put an alias in your start up script: alias cp='cp -i' You can put this directly in ~/.bashrc, but I have in my ~/.bashrc: if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi and in ~/.bash_aliases I have: alias realias='source ~/.bash_aliases' alias cp='cp -i' alias rm='rm -i' and when I have added/changed things to that file I do ...


9

You can use rainbow, which allows to colorize lines based on regular expressions: rainbow --red='SEVERE.*' --green='INFO.*' -- tail -f my-file.log It also comes bundled with predefined configs, for example for Tomcat logs: rainbow --config=tomcat -- tail -f my-file.log


9

Zsh has an sh compatibility mode which will let it execute POSIX sh code and some bash extensions. As long as you don't use bash features that zsh doesn't have (with the same syntax), you can have the same file sourced by both shells. Use the emulate built-in to put zsh in compatibility mode; with the -L option, the emulation is local to the enclosing ...


9

There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function: function make-completion-wrapper () { local function_name="$2" local arg_count=$(($#-3)) local comp_function_name="$1" shift 2 local function=" function $function_name { ((COMP_CWORD+=$arg_count)) COMP_WORDS=( ...


9

From Aliases (section 6.6 of the Bash Manual): The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. This happens when you use the alias, not when you define it. Here's an example: $ alias a1='a2 hello' $ alias a2='echo' $ a1 hello $ unalias a2 $ a1 bash: a2: ...



Only top voted, non community-wiki answers of a minimum length are eligible