Adding command shortcuts to /bin
An easy way for a shortcut is to define an alias
alias dcls='docker container ls'
This will execute docker container ls
when you enter dcls
and the command alias
lists your defined aliases. To remove this alias use unalias dcls
.
If you use bash, you can save the alias in your ~/.bashrc
or ~/.bash_aliases
.
If your ~/.bash_aliases
is not read on startup, you can add this line to your ~/.bashrc
:
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
Actually, what you describe would work, with a few notes:
- You could simply put
docker container ls
into a file called/bin/dcls
. But the behavior of that can be a little complicated. It’s a little more reliable to begin the file with a line called a “shebang”, so the file would look like#!/bin/sh docker container ls
which specifies that the file is a shell script. - Before you can run the command,
you must make the file executable with a command like
chmod +x /bin/dcls
You probably need to be root to do this (i.e., run it withsudo
). Follow the above two steps and you will be able to type
dcls
and it will dodocker container ls
. But, if you typedcls -l foo
, it will still dodocker container ls
. If you want it to dodocker container ls -l foo
, you should change the script to say#!/bin/sh docker container ls "$@"
which specifies that any arguments that you type on thedcls
command line should be passed along to thedocker container ls
command.Naturally, there are more complicated things you can do with command-line arguments.
For a mapping of one simple command → one simple command,
that doesn’t need to be shared with other users,
it’s simpler to define an alias (as Freddy suggested),
or a shell function.
More complicated functions are often written as scripts;
i.e., text files that contain commands.
But, if you don’t need to share it with other users,
it’s more common to use a private bin
directory.
$ cd # (to your home directory) $ mkdir binThen copy
dcls
to $HOME/bin
,
and addexport PATH="$HOME/bin:$PATH"to your
~/.bashrc
.
Also, it’s common to put personal scripts into /usr/local/bin
,
and leave /bin
for the programs that came with the system.