Tag Info

Hot answers tagged

114

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


107

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


97

You can also prefix a back slash to disable the alias: \ls Edit: Other ways of doing the same include: Use "command": command ls as per Mikel. Use the full path: /bin/ls as per uther. Quote the command: "ls" or 'ls' as per Mikel comment. You can remove the alias temporarily for that terminal session with unalias command_name.


88

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


76

If you're just switching between two directories, you can use cd - to go back and forth.


44

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


41

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.


39

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


38

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


38

With the assumption that you call vi with the directory as the last argument: vi() { if [[ -d ${!#} ]]; then cd "$@" else command vi "$@" fi }


33

grc, the generic colouriser is pretty cool http://korpus.juls.savba.sk/~garabik/software/grc.html Just do grc tail -f /var/log/apache2/error.log and enjoy


32

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


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


26

MacOS: alias ll='ls -lG' Linux: alias ll='ls -l --color=auto' Stick that in ~/.bashrc.


25

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


25

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: " ...


25

Have the shell list the dot files, and tell ls not to see through directories: ls -d .*


24

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


24

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


24

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


23

!! is expanded by bash when you type it. It's not expanded by alias substitution. You can use the history built-in to do the expansion: alias sbb='sudo $(history -p !!)' If the command is more than a simple command (e.g. it contains redirections or pipes), you need to invoke a shell under sudo: alias sbb='sudo "$BASH" -c "$(history -p !!)"'


21

In bash there is also autocd option. You can enable it by using shopt -s autocd: pbm@tauri ~ $ shopt -s autocd pbm@tauri ~ $ django # Now just type this cd ./django <- it's done automatically pbm@tauri ~/django $


20

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


19

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


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


19

strace doesn't run rm -i for the same reason than: echo rm doesn't output rm -i. Aliases are a feature of a few shells to allow some strings to automatically replaced by another when found in command position. In: alias foo='whatever' foo xxx The shells expands that to: whatever xxx and that undergoes another round of interpretation, in that case ...


18

Alias in bash can't have arguments, but you can use functions. e() { emacs "$@" & } then e foo.txt will do what you want.


17

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


17

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


15

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



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