Set parameters for Ubuntu's alias

I have an example command as follows:

g++ main.cpp -o main -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -I/usr/local/include/opencv4

Running the entire above command will create file main based on the second argument main after the parameter -o. I have reset it in file .zshrc as follows:

alias ocv='f(){ g++ "$@" -o built_$@ -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -I/usr/local/include/opencv4; unset -f f; }; f'

Now run the above command as follows:

ocv main.cpp

It will create a file called built_main.cpp. But I want it to generate the file main by removing the extension .cpp. How to do it?


Solution 1:

For zsh you should use modifier :r on $@, see e.g. this.

So it would be

alias ocv='f(){ g++ "$@" -o built_$@:r -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -I/usr/local/include/opencv4; unset -f f; }; f'