I usually optimize images locally before uploading them to the live servers for my websites. Since I use Linux Mint, one of my regular tools for this job is cwebp, which I run directly from the command line to convert PNG and JPG images to WebP.
My most-used command is simple:
cwebp image-name.png -o image-name.webp
It works well, but after using it again and again, I started looking for a way to avoid typing the same filename twice every time I converted an image. When filenames are long, that small repetition becomes even more annoying.
Because I already use the Linux terminal for this task, I wanted a solution that would stay simple and keep using cwebp rather than adding another application. I solved it by creating a small Bash function called towebp.
Now, instead of writing the full command, I can simply run:
towebp image-name.png
and Bash automatically creates:
image-name.webp
This article explains the setup I use on Linux Mint, how the Bash function works, and how you can add the same shortcut to your own terminal.
Check That cwebp Is Installed
Before creating the shortcut, first confirm that cwebp is installed and available from your terminal.
Run:
which cwebp
On my Linux Mint system, the output is:
/usr/bin/cwebp
This confirms that the cwebp executable is installed and available in the shell path.
If the command returns nothing, install the WebP command-line utilities before continuing.
The Normal cwebp Command
The general cwebp command can include compression options and a quality value:
cwebp [options] -q quality input.png -o output.webp
For a basic conversion, I usually use:
cwebp image-name.png -o image-name.webp
The command is easy enough, but the filename has to be written twice.
For example, if the source image is:
php-allowed-memory-size-exhausted-real-fix.png
the full command becomes:
cwebp php-allowed-memory-size-exhausted-real-fix.png -o php-allowed-memory-size-exhausted-real-fix.webp
That is more typing than I want for a task I perform regularly.
Create a Short towebp Command with a Bash Function
Bash functions are a good fit for this because the shortcut needs to accept a filename, remove its current extension, and automatically create the WebP output filename.
Open your Bash configuration file:
nano ~/.bashrc
Go to the very bottom of the file and add:
# Convert image to WebP
towebp() {
cwebp "$1" -o "${1%.*}.webp"
}
Then save the file in Nano:
- Press
Ctrl + O - Press
Enter - Press
Ctrl + X
Reload the Bash Configuration
After changing ~/.bashrc, reload it with:
source ~/.bashrc
You can also close the terminal and open a new one. In my case, I reopened the terminal to make sure the updated Bash configuration was loaded cleanly.
That also resolved an initial towebp: command not found message I saw after adding the function.
Use the New towebp Command
Once the function is loaded, switch to any directory that contains the image you want to convert.
For example:
cd ~/Downloads/website-images
Then run:
towebp file-name.png
If the source file is:
file-name.png
the function creates:
file-name.webp
The original image remains unchanged.
How the Bash Function Works
The function contains this command:
cwebp "$1" -o "${1%.*}.webp"
There are two important parts to understand.
What “$1” Means
Inside a Bash function, $1 represents the first argument passed to the function.
If you run:
towebp photo.png
then:
$1 = photo.png
The quotes around "$1" matter because they allow filenames containing spaces to be handled correctly.
What “${1%.*}” Means
The expression:
${1%.*}
removes the shortest matching .* suffix from the filename. In this case, that effectively removes the existing file extension.
For example:
photo.png
becomes:
photo
The function then adds .webp, producing:
photo.webp
It Also Works with JPG and JPEG Files
The function is not limited to PNG files.
You can run:
towebp photo.jpg
and it creates:
photo.webp
The same approach works with JPEG files:
towebp photo.jpeg
which also creates:
photo.webp
Using Filenames with Spaces
The function can also handle filenames containing spaces, as long as you quote the filename when calling it.
For example:
towebp "My Website Image.png"
This creates:
My Website Image.webp
Add a Default WebP Quality Setting
If you normally use the same quality setting for most of your website images, you can include that value directly in the function.
For example, to always use quality 82:
# Convert image to WebP using quality 82
towebp() {
cwebp -q 82 "$1" -o "${1%.*}.webp"
}
Now this:
towebp image.png
runs the equivalent of:
cwebp -q 82 image.png -o image.webp
This is useful if you want to keep a consistent WebP compression setting across images prepared for your websites.
Troubleshooting: towebp Command Not Found
If you run the shortcut and see:
towebp: command not found
first confirm that the function was actually saved in ~/.bashrc.
grep -n -A3 'towebp' ~/.bashrc
You should see something similar to:
120:towebp() {
121- cwebp "$1" -o "${1%.*}.webp"
122-}
Next, check whether Bash has loaded the function:
type towebp
If it is loaded correctly, Bash should report:
towebp is a function
You can also check your ~/.bashrc file for Bash syntax errors:
bash -n ~/.bashrc
If there are no syntax errors, the command normally produces no output.
If the function is present in ~/.bashrc but still is not available, close the current terminal window and start a new one. That fixed the problem in my setup.
Why Use a Bash Function Instead of an Alias?
A Bash alias is useful when you want to replace one fixed command with another fixed command. This shortcut needs a little more logic because it has to accept an input filename and build a new output filename automatically.
A function handles that much more cleanly.
It lets this:
towebp image.png
automatically behave like:
cwebp image.png -o image.webp
without requiring you to write the output filename yourself.
Final Bash Function
If you only need the basic version, this is the complete function:
# Convert image to WebP
towebp() {
cwebp "$1" -o "${1%.*}.webp"
}
Add it to:
~/.bashrc
Then reload Bash:
source ~/.bashrc
or close the terminal and open a new one.
After that, converting an image becomes:
towebp file-name.png
and the output file is created as:
file-name.webp
Conclusion
The standard cwebp command already does its job well, but if you use it frequently, repeating the same filename for input and output becomes unnecessary work.
A small Bash function solves that neatly. The towebp command keeps the original filename, removes the old extension, and creates a WebP file with the same base name.
For anyone who regularly prepares images locally before uploading them to WordPress or another website, this is a simple Linux Mint customization that makes the command-line workflow faster and easier to use.
