Bash - Alias (of a command) - Builtin command
About
Alias allows to define shortcuts and synonyms for commonly used:
- shell commands (of group of command)
- or script
Articles Related
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
Example
For instance, the Syntax is
- for a command:
alias AliasName='command -arguments'
- for a script
alias AliasName='FullPathScript -arguments'
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"