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 with sudo
).
Follow the above two steps and you will be able to type dcls
and it will do docker container ls
.
But, if you type dcls -l foo
, it will still do docker container ls
.
If you want it to do docker 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 the dcls
command line
should be passed along to the docker 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 bin
Then copy
dcls
to
$HOME/bin
,
and add
export 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.
/bin
, you can add to~/bin
(a bin in your own directory), or to/usr/local/bin
(to share with all users of your system, but not interfere with package manager). Or better in this case, to use an alias. – ctrl-alt-delor May 03 '19 at 08:45