The better way to shorten Linux commands (it's not alias)
Source: Dev.to
Why Aliases Aren’t the Best Way to Shorten Commands
If you’re a new Linux user, you might have a ~/.bash_aliases file that looks like this:
alias apup='sudo apt update'
alias apug='sudo apt upgrade'
alias apls='apt list'
alias apsr='apt search'
While aliases can be handy for shortening lengthy commands, they have several drawbacks.
Too Many Aliases Clutter Autocompletion
In most shells, pressing Tab shows autocompletion suggestions. When many aliases share the same prefix, the suggestion list becomes noisy:
user@host:~$ ap
apls apsr
apt apug
apt-add-repository apup
apt-cache apt-get
aptitude apt-mark
Aliases Can’t Accept Parameters in the Middle
Aliases only allow arguments after the expanded command. Commands that require arguments in the middle (e.g., ffmpeg, magick) can’t be effectively shortened with an alias.
Aliases Hide the Command’s Syntax
alias gtad='git add .'
alias gtcm='git commit -m '
alias gtps='git push origin main'
These shortcuts prevent you from learning the actual command structure. For instance, gtad can’t add a specific file, so you’ll still need to type the full command in many cases. Hard‑coded arguments (like a username in a MySQL login) also become a limitation:
alias sql='sudo mysql -u root -p'
When an Alias Might Still Be Useful
If a command is always used with the same arguments—e.g., logging into a MySQL shell—you can keep a simple alias:
alias la='ls -A' # already present in many default ~/.bashrc files
But for anything that requires flexibility, consider a wrapper script instead.
Using Wrapper Scripts for Flexibility
A wrapper script can read parameters, perform logic, and still provide a short command name. Here’s an example for Git that injects a token into the remote URL:
#!/usr/bin/env bash
# Save this as ~/.local/bin/git (make sure ~/.local/bin is in $PATH)
if [[ $1 == "sync" ]]; then
# $2 should be /
/usr/bin/git remote set-url origin "https://$GIT_TOKEN@github.com/$2"
# add any additional steps here
else
/usr/bin/git "$@"
fi
- Add
export GIT_TOKEN=your_token_hereto~/.bashrc(or another secure location) so the token is available to the script. - By naming the script
gitand placing it earlier in$PATHthan/usr/bin, it overrides the system Git binary for the custom behavior while still delegating all other commands to the real Git.
Now you can run:
git sync username/repo-name
The same approach works for other tools (e.g., magick, yt-dlp) by adjusting the argument parsing logic.
If you’d like to see more of my Git wrapper scripts, check them out here.