Bash

Bash AliasingBash ArithmeticBash ArraysBash Associative arraysBash Avoiding date using printfBash Brace ExpansionBash Case statementBash CGI ScriptsBash Chain of commands and operationsBash Change shellBash co-processesBash Color script output (cross-platform)Bash Conditional ExpressionsBash Control StructuresBash Copying (cp)Bash Creating directoriesBash Customizing PS1Bash Cut CommandBash DebuggingBash Decoding URLBash Design PatternsBash File execution sequenceBash File Transfer using scpBash FindBash FunctionsBash getopts : smart positional-parameter parsingBash global and local variablesBash GrepBash Handling the system promptBash Here documents and here stringsBash history substitutionsBash Internal variablesBash IntroductionBash Job ControlBash Jobs and ProcessesBash Jobs at specific timesBash Keyboard shortcutsBash Listing FilesBash Managing PATH environment variableBash MathBash NamespaceBash Navigating directoriesBash on Windows 10Bash ParallelBash Parameter ExpansionBash Pattern matching and regular expressionsBash PipelinesBash PitfallsBash Process substitutionBash Programmable completionBash QuotingBash Read a file (data stream, variable) line-by-line (and/or field-by-field)Bash RedirectionBash ScopingBash Script shebangBash Scripting with ParametersBash Select keywordBash Sleep utilityBash SourcingBash Splitting FilesBash straceBash the cut commandBash true, false and : commandsBash Type of ShellsBash Typing variablesBash Using catBash Using sortBash Using trap to react to signals and system eventsBash when to use evalBash Word splittingNetworking with Bash



Bash Aliasing

From WikiOD

Shell aliases are a simple way to create new commands or to wrap existing commands with code of your own. They somewhat overlap with shell functions, which are however more versatile and should therefore often be preferred.

Remarks[edit | edit source]

The alias will only be available in the shell where the alias command was issued.

To persist the alias consider putting it into your .bashrc

Bypass an alias[edit | edit source]

Sometimes you may want to bypass an alias temporarily, without disabling it. To work with a concrete example, consider this alias:

alias ls='ls --color=auto'

And let's say you want to use the ls command without disabling the alias. You have several options:

  • Use the command builtin: command ls
  • Use the full path of the command: /bin/ls
  • Add a \ anywhere in the command name, for example: \ls, or l\s
  • Quote the command: "ls" or 'ls'

Create an Alias[edit | edit source]

alias word='command'

Invoking word will run command. Any arguments supplied to the alias are simply appended to the target of the alias:

alias myAlias='some command --with --options'
myAlias foo bar baz

The shell will then execute:

some command --with --options foo bar baz

To include multiple commands in the same alias, you can string them together with &&. For example:

alias print_things='echo "foo" && echo "bar" && echo "baz"'

Remove an alias[edit | edit source]

To remove an existing alias, use:

unalias {alias_name}

Example:

# create an alias    
$ alias now='date'

# preview the alias
$ now
Thu Jul 21 17:11:25 CEST 2016

# remove the alias
$ unalias now

# test if removed
$ now
*bash: now: command not found

List all Aliases[edit | edit source]

alias -p

will list all the current aliases.

Expand alias[edit | edit source]

Assuming that bar is an alias for someCommand -flag1.

Type bar on the command line and then press Ctrl+alt+e

you'll get someCommand -flag1 where bar was standing.

The BASH_ALIASES is an internal bash assoc array[edit | edit source]

Aliases are named shortcuts of commands, one can define and use in interactive bash instances. They are held in an associative array named BASH_ALIASES. To use this var in a script, it must be run within an interactive shell

#!/bin/bash -li
# note the -li above! -l makes this behave like a login shell
# -i makes it behave like an interactive shell
#
# shopt -s expand_aliases will not work in most cases

echo There are ${#BASH_ALIASES[*]} aliases defined.

for ali in "${!BASH_ALIASES[@]}"; do
   printf "alias: %-10s triggers: %s\n" "$ali" "${BASH_ALIASES[$ali]}" 
done

Credit:Stack_Overflow_Documentation