alias apup='sudo apt update'
alias apug='sudo apt upgrade'
alias apls='apt list'
alias apsr='apt search'
If you're a new Linux user, you might have a ~/.bash_aliases like this. Aliases are useful when you want to shorten lengthy commands, but aliasing like this isn't the best method for a few reasons.
Why not alias?
Too many aliases mess with autocompletion
In most Linux shells, pressing the tab key when typing out a command shows autocompletion suggestions. However, too many aliases beginning with the same prefix can clutter up these suggestions.
user@host:~$ ap
apls apsr
apt apug
apt-add-repository apup
apt-cache apt-get
aptitude apt-mark
user@host:~$ ap
Aliases don't support taking parameters
Sure, aliases allow arguments at the end of the expanded command, but not in between the command. This limits the kinds of commands that can be shortened using an alias. Hence, ffmpeg or magick can't be shortened this way since they take arguments for an input as well as output file.
Aliases hide the command's syntax
alias gtad='git add .'
alias gtcm='git commit -m '
alias gtps='git push origin main'
Aliases like these prevent you from learning the actual command's structure. For example, to commit only a specific file, gtad won't work, so you'll have to type out the whole command anyway. In such cases where the hardcoded arguments may change, learning the actual command is better than aliasing it and forgetting it.
When should you alias?
alias sql='sudo mysql -u root -p'
This is a good example of an alias. Once you've set up MySQL, you'll only ever use the command to login to the interactive shell. ~/.bashrc by default also contains some useful aliases like
alias la='ls -A'
However, sql alias is still a bit restrictive, since the username is hardcoded. These limitations can be addressed with...
Wrapper scripts!
These provide the complete flexibility of Bash (or whatever shell you're using). You can read parameters, perform operations within the script, and much more.
Let's take Git for example. You might always run certain commands with fixed parameters, such as using your token to connect to a Git repo. A wrapper script could be used to simplify this.
Create a script at ~/.local/bin. Don't forget to set GIT_TOKEN in ~/.bashrc or somewhere else. This way, such variables are kept in one place, making them easy to modify if needed.
#!/bin/bash
if [ $1 == "sync" ]; then
/usr/bin/git remote set-url origin https://$GIT_TOKEN@github.com/$2
# one more thing...
To make this wrapper script seem seamless, we can even change its name to git. This overrides the Git binary in /usr/bin. Add an else statement to let it handle all other parameters as usual.
# add this as well
else
/usr/bin/git "$@"
fi
Now, to connect to a repo, just type
git sync [github_username]/[repo_name]
These scripts can also be modified to match the structure of the other commands of the package (for example, choosing between sync, -sync, or -s depending on if it's git, magick, or yt-dlp).
(Btw, if you wanna see more of my Git wrapper scripts, check them out here!)
Top comments (0)