About
Alias allows to define shortcuts and synonyms for commonly used:
- shell commands (of group of command)
- or script
They allow a string to be substituted for a word when it is used as the first word of a simple command.
The shell maintains a list of aliases that may be:
- created and listed with the alias built-in command
- unset/removed with the unalias built-in commands.
Example
For instance, the Syntax is
- for a command:
alias AliasName='command -arguments'
- for a script
alias AliasName='FullPathScript -arguments'
Syntax
The basic syntax is:
alias [-p] [name[=value] ...]
where:
- p is to only print the aliases
- [name[=value] …] are arguments. When they are supplied, an alias is defined for each name whose value is given.
And:
- value: A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded.
- name: For each name in the argument list for which no value is supplied, the name and value of the alias is printed.
Return value:
- Alias returns true unless a name is given for which no alias has been defined.
Note aliases are not expanded by default in non-interactive shell, and it can be enabled by setting the expand_aliases shell option using shopt.
Configuration
Expansion
To use alias in script:
shopt -s expand_aliases
Expansion
Rules
- 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.
- The characters /, $, ‘, = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.
- The replacement text may contain any valid shell input, including shell metacharacters.
- The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to ls -F, for instance, and bash does not try to recursively expand the replacement text.
- If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.
- There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used.
- Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt
- For almost every purpose, aliases are superseded by shell functions.
Alias Expansion Process
- Bash always reads at least one complete line of input before executing any of the commands on that line.
- Aliases are expanded when a command is read, not when it is executed.
- Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read.
- The commands following the alias definition on that line are not affected by the new alias.
- This behavior is also an issue when functions are executed.
Aliases in function
- Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command.
- As a consequence, aliases defined in a function are not available until after that function is executed.
- To be safe, always put alias definitions on a separate line, and do not use alias in compound commands.
Management
Made the aliases permanent
Aliases can be put in the startup script in order to become permanent.
Extended Example from the OBIEE VM.
#alias created
alias startWLS='/bishiphome/Middleware/user_projects/domains/bifoundation_domain/bin/startWebLogic.sh'
alias stopWLS='/bishiphome/Middleware/user_projects/domains/bifoundation_domain/bin/stopWebLogic.sh'
alias startOPMN='/bishiphome/Middleware/instances/instance1/bin/opmnctl startall'
alias stopOPMN='/bishiphome/Middleware/instances/instance1/bin/opmnctl stopall'
alias statusOPMN='/bishiphome/Middleware/instances/instance1/bin/opmnctl status -l'
alias stopOBIPS='${FMW_HOME}/instances/instance1/bin/opmnctl stopproc ias-component=coreapplication_obips1'
alias startOBIPS='${FMW_HOME}/instances/instance1/bin/opmnctl startproc ias-component=coreapplication_obips1'
alias restartOBIPS='stopOBIPS && startOBIPS'
alias startESSB='/epm/Middleware/user_projects/epmsystem1/bin/startEssbase.sh'
alias stopESSB='/epm/Middleware/user_projects/epmsystem1/bin/stopEssbase.sh'
alias startAPS='/epm/Middleware/user_projects/epmsystem1/bin/startAnalyticProviderServices.sh'
alias stopAPS='/epm/Middleware/user_projects/epmsystem1/bin/stopAnalyticProviderServices.sh'
alias startOIDopmn='/idm/Middleware/asinst_1/bin/opmnctl start'
alias startOID='/idm/Middleware/asinst_1/bin/opmnctl startproc ias-component=oid1'
alias stopOID='/idm/Middleware/asinst_1/bin/opmnctl stopall'
alias statusOID='/idm/Middleware/asinst_1/bin/opmnctl status -l'
Lists the current aliases
alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Remove an alias
unalias myALiasName
Shortcut the frequently used command
Example:
- To start services such as OPMN
alias startOPMN ='/bishiphome/Middleware/instances/instance1/bin/opmnctl startall'
- To edit a configuration file
alias editHttpd="gedit /etc/httpd/conf/httpd.conf"
Specify the default parameters
If you want ls to show colors by default with the ls command:
alias ls='ls --color=yes'
Call an Alias with parameters
Alias doesn't support parameter whereas a function does.
Having a function in your startup script will permit to call them like alias.
For instance, to use ssh to copy files to a location on a server you can use this function
sendpic() { scp $* [email protected]:/www/misc/Pictures/; }
Standardize the name of commands across multiple operating systems
For people accustomed to MS-DOS commands, the following aliases can be defined so that a Unix-like operating system appears to behave more like MS-DOS:
alias dir="ls"
alias copy="cp"
alias rename="mv"
alias md="mkdir"
alias rd="rmdir"
alias del="rm -i"